Investigating memory with GDB
With GDB, we can learn more about how things work under the hood, for example, strings. A string is an array of characters terminated by a null character. In this recipe, we'll investigate a character array with GDB and see how the null character ends a string.
Knowing how to examine the memory using GDB can be really handy if you encounter weird bugs. Instead of guessing or looping over each character in C, we can directly examine them in GDB.
Getting ready
For this recipe, you'll need the Makefile we wrote in the Starting GDB recipe. You'll also need the GCC compiler and the Make tool.
How to do it…
In this recipe, we'll write a simple program that fills a character array with the character x. Then we'll copy a new, shorter string on top of that and finally print the string. It's only the newly copied string that is printed, even if all the x characters are still there. With GDB, we can confirm this...