Finding the Address of a Local Variable in C Using GDB

Say I have C code that goes line by line:

void fun_1 (unsigned int * age)

[...]

int main () {

    unsigned int age [24];
}

In GDB, how can I find the age address?

+5
source share
2 answers

Finding an address is as simple as:

p &age
+9
source

Both agedo not match if you do not know. One of them is local to main, and the other is local to fun_1(). Therefore, if you do not give an address agein mainto fun_1(), they will not have the same address. Just set a breakpoint in the main and see the address of age.

(gdb) break main
(gdb) p &age
.....
(gdb) break fun_1
(gdb) p &age
.....
+5
source

All Articles