Using getopt with gdb

just included getoptin my main()func

getoptsets a global variable optargfor each call

going through main()with gdb, after the getopt()call is optargalways NULL(for example (gdb) p optarg), but printf("%s\n", optarg)displays the cmd line as expected

What's happening? why are these two not the same?

Is this isue with gdb and how is it trying to check global variables or is something else going on?

+5
source share
1 answer

Probably related: Error 13800 - gdb does not print the correct values ​​for getopt-related values

Also note that:

(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

Where:

(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);
+2
source

All Articles