Find the exact address of variable Buf

Neefra picture Neefra · Dec 16, 2010 · Viewed 75.7k times · Source

As reference, I'm using the following code:

#include <stdio.h>
#include <string.h>

int main (void) {
    char buf[100]; // ------> How do I find the address in gdb?

    printf ("Buffer is at memory location: %08x\n", &buf);
    strcpy (buf, "some random text");
    printf ("Text is [%s]\n", buf);

    return 0;
}

How can I get gdb to show me the address of the buf variable?

Answer

Nikolai Fetissov picture Nikolai Fetissov · Dec 16, 2010

(gdb) p &a if you need the address of variable a. A variable might be cached in a register though, in which case GDB would tell you address requested for identifier "a" which is in register $xxx.

Sidenote: do not use gets, see here.