2012-03-16 28 views
5

使用的getopt剛剛納入getopt到我main() FUNC使用gdb

getopt設置全局變量optarg每個呼叫

通過main()gdb步進,之後getopt()呼叫optarg總是NULL(如(gdb) p optarg),但printf("%s\n", optarg)按預期輸出cmd線arg

最新情況如何?爲什麼這兩個不一樣?

這是一個與gdb的isue,它如何檢查全局或其他事情正在進行?

回答

2

可能與: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);