简体   繁体   English

为什么不从int的地址回​​读给我int的正确值(C&gdb)

[英]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. 我有使用gdb调试的ac程序。 This is done on Ubuntu x86 and c program is compiled with gcc. 这是在Ubuntu x86上完成的,并且使用gcc编译了c程序。 I declare two int variables one after other 我一个接一个地声明两个int变量

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

The values I pass to these are 2 and 4 respectively. 我传递给这些的值分别是2和4。 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. 根据以下数据,似乎int b从地址0xbffff048开始,并占用0xbffff048、0xbffff049、0xbffff04a,0xbffff04b。 And it also makes sense that int b starts at the next address 0xbffff04c. 而且int b从下一个地址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). 但是,如果我显示这四个地址的值,则返回的十六进制值不等于十进制4(这是在应用程序中确认并在gdb调试中打印的b值)。 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. 您看到的值2在整数指定的4个字节之后。 You have your input 4 right there occupying the first nibble. 您的输入4就在那里占据第一个半字节。

I think comments explain it even better. 我认为评论可以更好地解释它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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