Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding address of a local variable in C with GDB

Tags:

c

gdb

Say I have some C code which goes along the lines of:

void fun_1(unsigned int *age)

[...]

int main() {

    unsigned int age[24];
}

In GDB, how can I find the address of age?

like image 672
Daveid Fred Avatar asked May 31 '12 14:05

Daveid Fred


2 Answers

Finding address is as simple as:

p &age
like image 61
Andrejs Cainikovs Avatar answered Oct 21 '22 02:10

Andrejs Cainikovs


Both ages are not the same in case if you are not aware. One is local in main and another is local to fun_1(). So unless you pass the address of age in main to fun_1() they are not going to have the same address. Just set a break point in main and see the address of age.

(gdb) break main
(gdb) p &age
.....
(gdb) break fun_1
(gdb) p &age
.....
like image 25
P.P Avatar answered Oct 21 '22 01:10

P.P