gpt4 book ai didi

c - 将 getopt 与 gdb 一起使用

转载 作者:太空狗 更新时间:2023-10-29 15:07:32 25 4
gpt4 key购买 nike

刚刚将 getopt 合并到我的 main() 函数中

getopt 为每次调用设置全局变量optarg

使用 gdb 单步执行 main(),在 getopt() 调用 optarg 之后总是 NULL(例如 (gdb) p optarg)但 printf("%s\n", optarg) 按预期输出 cmd 行 arg

这是怎么回事?为什么两者不一样?

这是 gdb 的问题吗?它是如何尝试检查全局变量的,还是发生了其他问题?

最佳答案

可能相关:Bug 13800 - gdb does not print right values of getopt-related values

另请注意:

(gdb) n
opt: 111, arg,
0x804a040
0x804a034
0x804a020
0x804a030

(gdb) printf "%p\n%p\n%p\n%p\n", &optarg, &opterr, &optind, &optopt
0x2ae760
0x2ab0f4
0x2ab0f8
0x2ab0f0

地点:

(gdb) l
6 int main(int argc, char *argv[])
7 {
8 int c;
9 while ((c = getopt(argc, argv, ":abf:o:")) != -1) {
10 printf("opt: %d, %s, \n"
11 "%p\n%p\n%p\n%p\n",
12 c, optarg,
13 &optarg, &opterr, &optind, &optopt);

关于c - 将 getopt 与 gdb 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9736264/

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