Memory Pointers Example: Memory Layout
Learn about memory layout and pointers with the help of an example.
Memory pointers in the C/C++ language
We have the following data declaration and definition in the C/C++ language:
Memory pointers in the assembly language
The example code we’ll look at in this lesson corresponds to the following pseudocode and assembly language:
Source code
The source code of memory pointers is as follows:
Memory pointers in the GDB disassembly
The code example of memory pointers is described in detail below.
Compile and execute GDB commands
We compile and link the course code, and load the executable into GDB. We get the following output:
Note: You can practice all the commands in the coding playground provided at the end of the lesson.
Now, we are in GDB environment:
We then put a breakpoint on the main function and run the program until GDB reaches at this point:
A breakpoint is shown below:
Now, run the program:
The output after execution is given below:
Now, we disassemble the main function:
The output is given below:
We then clear x0 – x3 registers to set up a memory layout that is easy to follow:
We get the following output:
We also instruct GDB to automatically display the current instruction to be executed, the values of registers x0 – x3, and the contents of variables a, b, pa, pb:
We see that the pb variable contains the address 0x4100f4. We then check the addresses of (variables) memory locations a, b, pa, pb:
We also check the value stored at the address 0x4100f4 (value of pb that is the address of ...