c - 为什么gdb一直显示optarg为0x0

标签 c gdb getopt getopt-long

<分区>

我正在学习 getopt 和 *getopt_long* 的工作原理。一个问题是,当我使用 gdb 逐步运行以下简单程序时,optarg 始终为 0x0。
知道为什么吗?是不是gdb的问题?
我试图在网上搜索并查看程序的汇编代码,但还没有找到答案。
调试代码表明 optarg 按预期指向 agv[3](值“16”)。

#include <unistd.h>
#include <getopt.h>
#include <stdio.h>
static struct option options[] =
{
  /* name     has_arg flag  val */
  { "num",  1, NULL, 'n' },
  { NULL ,  0, NULL , 0  }
};

int main(int argc, char** argv)
{
  int option;
  option = getopt_long( argc, argv, "n:", options, NULL );
  if (option == 'n') {
    printf("%s\n", optarg);
    if (optarg == NULL)
      printf("optarg == NULL\n");
    if (optarg == 0)
      printf("optarg == 0\n");
  }

  return 0;
}

在 gdb 上:

    (gdb) run -n 16
    Starting program: /home/ducalpha/clang/misc/a.out -n 16
...
    16          printf("%s\n", optarg);
    (gdb) p optarg
    $1 = 0x0

输出:

$ ./a.out -n 16
16

最佳答案

(针对修改后的问题修改后的答案)

它对我有用。输出是:

Reading symbols from /home/wally/a.out...done.
(gdb) run -n 16
Starting program: /home/wally/a.out -n 16
16
[Inferior 1 (process 10999) exited normally]

关于c - 为什么gdb一直显示optarg为0x0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12311157/

相关文章:

c++ - C/C++/Objective-C 文本识别库

calloc 调用失败,代码崩溃

gdb - 使用Eclipse通过gdbserver进行远程调试

bash - getopt后 'set -- $args'的作用是什么?

c - Getopt 转移 optarg

c - C 中的幂函数为大数提供相同的输出

c - 如何创建一个线程来处理信号并在收到信号后退出进程?

c - 使用GDB运行时 fatal error 消失

perl - 如何将强制和可选命令行参数传递给 perl 脚本?

c - GCC fatal error : stdio. h: 没有那个文件或目录