I'm new to GDB and is curious about if varaible's address will change or not, during different debugging?
The code I'm using:
#include <stdio.h>
int main()
{
char * p = malloc(10);
printf("heap=%p stack=%p\n", p, &p);
}
Compile:gcc main.c -g
And for 3 times in my Ubuntu, the GDB console, all gives same:
gdb$ b 5
Breakpoint 1 at 0x4005fb: file main4.c, line 5.
gdb$ r
Starting program: /home/zz/work/bold/src/a.out
Breakpoint 1, main () at main4.c:5
gdb$ p &p
$1 = (char **) 0x7fffffffe060
However, running the compiled (debuggable) a.out
file twice, it gives different output for &p
:
heap=0x1c47010 stack=0x7ffd2df09b50
heap=0x25a5010 stack=0x7ffd757125f0
Will GDB guarantee any variable is with same address during different debugging time or not, and why?
Also, why just running instead of debugging seems using different scheme?