Tags: lec4

Q: You have a segfault.  How can you figure out what line of code is causing it?
A: 1. do a debug build by passing "-g" to g++
2. gdb YOURPROGRAM
3. enter "run"
4. enter "bt" to get a backtrace

Q: What are some reasons gdb might not be able to show you a line number causing a segfault?
A: Perhaps you did a release build (not a debug build with "-g").  The release build will be faster, but will not embed debug info in the binary.
Perhaps optimizations inlined or modified the function that contains the bug (e.g., if you used "-O2").

Q: Why should you NEVER return a pointer to a local variable from a function?
A: The local variable exists on the stack frame of the function, and the stack frame is deleted as soon as the function returns.  So you would be returning a pointer to invalid memory.  It might appear OK for a while (perhaps the memory hasn't been used by anything else yet), but if other functions are called, that memory will go to different stack frames, and you'll be pointing to a value you never intended.

Q: Why is nullptr (from C++) better that NULL (from C)?
A: NULL can be interpreted as 0 -- that can mess up which function gets called if you have overloaded functions.  If you're passing NULL, you probably want a function that takes a pointer (not an int) to be called.