I get a segfault related to the following line:
int testenum = atoi(argv[1]);
The following is the whole error:
/build/bin/wrapper' has changed; re-reading symbols.
Temporary breakpoint 5 at 0x13f5: file /examples/wrapper.cpp, line 166.
Starting program: /build/bin/wrapper
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/usr/lib/libthread_db.so.1".
Temporary breakpoint 5, main (argc=1, argv=0x7fffffffe288) at /examples/wrapper.cpp:166
166 testenum = atoi(argv[1]);
(gdb) c
Continuing.
Program received signal SIGSEGV, Segmentation fault.
0x00007ffff6fffa3b in __GI_____strtol_l_internal (nptr=0x0, endptr=0x0, base=10, group=<optimized out>, loc=0x7ffff71934a0 <_nl_global_locale>) at ../stdlib/strtol_l.c:291
291 while (ISSPACE (*s))
(gdb) backtrace
#0 0x00007ffff6fffa3b in __GI_____strtol_l_internal (nptr=0x0,
endptr=0x0, base=10, group=<optimized out>,
loc=0x7ffff71934a0 <_nl_global_locale>) at ../stdlib/strtol_l.c:291
#1 0x0000555555555405 in atoi (
__nptr=0xa <error: Cannot access memory at address 0xa>)
at /usr/include/stdlib.h:364
#2 main (argc=<optimized out>, argv=0x7fffffffe288)
at /examples/wrapper.cpp:166
(gdb)
Why does this seemingly simple line trigger this error in GDB, when it runs seemingly fine when ran traditionally?
The message tells you exactly what the error is:
Translation: The error happened while converting a string to long, the input string pointer (nptr) is NULL.
Since this line of code attempts to convert a string to long
This means argv[1] is NULL. You failed to check before passing command line arguments to atoi() that the user actually passed arguments to the program when launching it from the command line.
Use main function parameter argc to check if the user passed arguments to the program. To use argv[1], then you should have argc > 1, otherwise your program will crash and will print the error message above.