Click to See Complete Forum and Search --> : variable location in C


threadhead
02-20-2003, 05:53 PM
hi there.
ive been programming recently something to print me the memory
location of a variable.

heres my work so far (really much huh? :D)

#include <stdio.h>

int main (void)
{

char *temp;
char buffer = 'A';
temp = &buffer;


printf("location of temp: %p\n", temp);

return 0;
}



that prints out the memory location of the variable temp.
but when i run gdb on the executable, i cannot find that value again.

heres the ouput of that program:

%./test
location of temp: 0xbfbffbdb
%gdb test
GNU gdb 4.18 (FreeBSD)
Copyright 1998 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "i386-unknown-freebsd"...(no debugging symbols found)...
(gdb) disass main
Dump of assembler code for function main:
0x8048494 <main>: push %ebp
0x8048495 <main+1>: mov %esp,%ebp
0x8048497 <main+3>: sub $0x18,%esp
0x804849a <main+6>: movb $0x41,0xfffffffb(%ebp)
0x804849e <main+10>: lea 0xfffffffb(%ebp),%eax
0x80484a1 <main+13>: mov %eax,0xfffffffc(%ebp)
0x80484a4 <main+16>: add $0xfffffff8,%esp
0x80484a7 <main+19>: mov 0xfffffffc(%ebp),%eax
0x80484aa <main+22>: push %eax
0x80484ab <main+23>: push $0x80484f7
0x80484b0 <main+28>: call 0x8048358 <printf>
0x80484b5 <main+33>: add $0x10,%esp
0x80484b8 <main+36>: xor %eax,%eax
0x80484ba <main+38>: jmp 0x80484bc <main+40>
0x80484bc <main+40>: leave
0x80484bd <main+41>: ret
0x80484be <main+42>: mov %esi,%esi
End of assembler dump.
(gdb)


whats my problem? ;)
thank you

bwkaz
02-20-2003, 06:51 PM
It may be getting optimized away.

Try to compile with -O0 and see if that helps.

Actually:

Originally posted by threadhead

%./test
location of temp: 0xbfbffbdb
%gdb test
GNU gdb 4.18 (FreeBSD)
Copyright 1998 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "i386-unknown-freebsd"...(no debugging symbols found)...
(gdb) disass main
Dump of assembler code for function main:
0x8048494 <main>: push %ebp
0x8048495 <main+1>: mov %esp,%ebp
0x8048497 <main+3>: sub $0x18,%esp
0x804849a <main+6>: movb $0x41,0xfffffffb(%ebp)
0x804849e <main+10>: lea 0xfffffffb(%ebp),%eax
0x80484a1 <main+13>: mov %eax,0xfffffffc(%ebp)
0x80484a4 <main+16>: add $0xfffffff8,%esp
0x80484a7 <main+19>: mov 0xfffffffc(%ebp),%eax
0x80484aa <main+22>: push %eax
0x80484ab <main+23>: push $0x80484f7
0x80484b0 <main+28>: call 0x8048358 <printf>
0x80484b5 <main+33>: add $0x10,%esp
0x80484b8 <main+36>: xor %eax,%eax
0x80484ba <main+38>: jmp 0x80484bc <main+40>
0x80484bc <main+40>: leave
0x80484bd <main+41>: ret
0x80484be <main+42>: mov %esi,%esi
End of assembler dump.
(gdb) See the following explanation:

sub 0x18, %esp -- allocate space for all the local variables

movb $0x41,0xfffffffb(%ebp) -- put 'A' into buffer

lea 0xfffffffb(%ebp),%eax -- put the address of buffer (lea is load effective address) into eax

mov %eax,0xfffffffc(%ebp) -- store the address into temp

add $0xfffffff8,%esp -- allocate some more local variable space; I'm not quite sure why, maybe it has something to do with the way printf works?

mov 0xfffffffc(%ebp),%eax -- puts temp into eax

The rest of it pushes eax, pushes the address of the string, calls printf, cleans up after printf, and exits with a return status of 0.