Why doesn't reading back from the address of an int give me back the correct value of the int (C & gdb)

2

I have a c program that I am debugging with gdb. This is done on Ubuntu x86 and c program is compiled with gcc. I declare two int variables one after other

int a = atoi (argv[1]);
int b = atoi (argv[2]);

The values I pass to these are 2 and 4 respectively. Now I break after these lines and debug. Based on below data, it would seem that int b starts at address 0xbffff048 and occupies 0xbffff048, 0xbffff049, 0xbffff04a, 0xbffff04b. And it also makes sense that int b starts at the next address 0xbffff04c. But if I display the values of these four addresses, what I get back in hex is not equal to decimal 4(which is the value of b as confirmed in the app and as printed in the gdb debug). What am I interpreting/doing wrong here?

(gdb) display a
2: a = 2
(gdb) display b
3: b = 4
(gdb) display &a
4: &a = (int *) 0xbffff04c
(gdb) display &b
5: &b = (int *) 0xbffff048
(gdb) x  0xbffff048
0xbffff048:     0x00000004
(gdb) x  0xbffff049
0xbffff049:     0x02000000
(gdb) x  0xbffff04a
0xbffff04a:     0x00020000
(gdb) x  0xbffff04b
0xbffff04b:     0x00000200
c
x86
gdb
asked on Stack Overflow Jan 29, 2019 by Stubborn • edited Jan 29, 2019 by Stubborn

1 Answer

3

The value 2 you see is after the designated 4 bytes for the integer. You have your input 4 right there occupying the first nibble.

I think comments explain it even better.

answered on Stack Overflow Jan 29, 2019 by Bpaul

User contributions licensed under CC BY-SA 3.0