10

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?

Andrejs Cainikovs
  • 27,428
  • 2
  • 75
  • 95
Daveid Fred
  • 409
  • 1
  • 5
  • 11
  • 5
    "Finding address of function" - `age` is not a function in your code snippet. – Oliver Charlesworth May 31 '12 at 14:45
  • 4
    Which age? Wouldn't `p &age` work after you enter the any of the functions? Both have `age`. – sidyll May 31 '12 at 14:46
  • 1
    Plz, there are a lot of sites about simple gdb's commands out there. Try google before post at SO. ftp://ftp.gnu.org/pub/old-gnu/Manuals/gdb/html_node/gdb_109.html – André A. G. Scotá May 31 '12 at 15:02
  • Are you interested in the address of the local variable `age`, which just a pointer, or the address that `age` points to? – Brian McFarland May 31 '12 at 17:29
  • Does this answer your question? [Find the exact address of variable Buf](https://stackoverflow.com/questions/4462915/find-the-exact-address-of-variable-buf) – user202729 Dec 07 '21 at 01:48

2 Answers2

14

Finding address is as simple as:

p &age
Andrejs Cainikovs
  • 27,428
  • 2
  • 75
  • 95
7

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
.....
P.P
  • 117,907
  • 20
  • 175
  • 238