have just incorporated getopt
into my main()
func
getopt
sets the global variable optarg
for each call
stepping through main()
with gdb
, after getopt()
call optarg
is always NULL
(e.g. (gdb) p optarg
) yet printf("%s\n", optarg)
outputs the cmd line arg as expected
whats going on? why are the two not the same?
Is this an isue with gdb and how it trys to inspect globals or is something else going on?
Probably related: Bug 13800 - gdb does not print right values of getopt-related values
Also notice ie:
(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);
来源:https://stackoverflow.com/questions/9736264/using-getopt-with-gdb