gpt4 book ai didi

c++ - gdb: 当前上下文中没有符号 "tulip"

转载 作者:行者123 更新时间:2023-11-28 02:11:50 24 4
gpt4 key购买 nike

我正在尝试编译我的程序并使其可执行,但每次我尝试打印它时都会说:“当前上下文中没有符号“郁金香”。"

这是我的代码:

#include <stdlib.h>
#include <stdio.h>

int main() {
unsigned long *daffodil = (unsigned long *) malloc(sizeof(unsigned long));
if (daffodil == (unsigned long *) NULL)
exit(EXIT_FAILURE);
*daffodil = 170;
unsigned long *tulip = daffodil;
unsigned long *carnation = (unsigned long *) malloc(sizeof(unsigned long));
if (carnation == (unsigned long *) NULL)
exit(EXIT_FAILURE);
*carnation = 995;
unsigned long *lily = tulip;
unsigned long crocus = 953;
unsigned long *zinnia = &crocus;
(void) printf("Registering daffodil: %lu.\n", *daffodil);
(void) printf("Registering tulip: %lu.\n", *tulip);
(void) printf("Registering carnation: %lu.\n",*carnation);
(void) printf("Registering lily: %lu.\n", *lily);
(void) printf("Registering crocus: %lu.\n", crocus);
(void) printf("Registering zinnia: %lu.\n", *zinnia);
carnation = (unsigned long *) NULL;
free(daffodil);
free(carnation);

return EXIT_SUCCESS;
}

这是我在终端中所做的: enter image description here

最佳答案

为了在 GDB 中查看局部变量,您必须中断该函数内的程序执行。您正试图在程序终止后打印该值,但这是不可能的。

尝试在第 10 行放置一个断点,我认为:

(gdb) br 10
(gdb) run
(gdb) print tulip

您也可以在 main 的开头中断,然后继续前进直到变量初始化:

(gdb) br main
(gdb) run
(gdb) next
(gdb) next
(gdb) ...
(gdb) print tulip

main 处的断点非常频繁,以至于 gdb 有一个自动执行断点的命令:start

如果你对某个变量特别感兴趣,那么你可以显示它,这样它就会在每一步自动打印:

(gdb) start
(gdb) display tulip
(gdb) next
(gdb) next
(gdb) ...

关于c++ - gdb: 当前上下文中没有符号 "tulip",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35407741/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com