简体   繁体   中英

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

I have ac 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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM