Here, gdb is resolving wrong optind variable.
Change your loop to
Code:
while ( ( optc = getopt(argc, argv, ":b:B:h" ) ) != -1 ) {
printf( "%c %d %s %x %x\n", optc, optind, optarg,&optc,&optind);
}
And then run in gdb.
Now print the address of optind & optc in gdb (print &optind) as well as via this loop. You will get to know that address of optc is same but of optind, its something else.So, your program and gdb resolved to different addresses when they want to access the optind variable
Here, your optind is actually linked to optind@@GLIBC_2.0. In gdb write this:
(gdb) p optind
and hit the tab key (twice), it'll autocomplete the symbols. Here you can see, you have optind and optind@@GLIBC_2.0.
Now print address and value of optind@@GLIBC_2.0
(gdb) print 'optind@@GLIBC_2.0'
(gdb) print &'optind@@GLIBC_2.0'