检查整数和字符数组的内存时字节顺序不一致
Inconsistent byte order while examining memory of an integer and a char array
在 little endian 机器上,我正在尝试使用 GDB 检查以下变量的内存。
int main()
{
char buffer[4] = "1234";
int value = 0x31323334;
//ascii - "1 2 3 4"
retun 0;
}
我希望看到的是:
对于缓冲区变量 - 0x34333231
对于值变量 - 0x34333231
但是,GDB 检查输出是:
(gdb) show endian
The target endianness is set automatically (currently little endian)
(gdb) x/w &value
0x7fffffffe440: 0x31323334
(gdb) x/w buffer
0x7fffffffe444: 0x34333231
为什么在内存中存储int和char数组数据有区别?
看起来值变量存储为大端,我是否遗漏了什么?
变量buffer
就是四个字节
+------+------+------+------+
| 0x31 | 0x32 | 0x33 | 0x34 |
+------+------+------+------+
^ ^
| |
Low address High address
变量value
就是四个字节
+------+------+------+------+
| 0x34 | 0x33 | 0x32 | 0x31 |
+------+------+------+------+
^ ^
| |
Low address High address
并且 x
命令以本机字节顺序显示(在您的情况下为小端),导致您获得输出。
在 little endian 机器上,我正在尝试使用 GDB 检查以下变量的内存。
int main()
{
char buffer[4] = "1234";
int value = 0x31323334;
//ascii - "1 2 3 4"
retun 0;
}
我希望看到的是:
对于缓冲区变量 - 0x34333231 对于值变量 - 0x34333231
但是,GDB 检查输出是:
(gdb) show endian
The target endianness is set automatically (currently little endian)
(gdb) x/w &value
0x7fffffffe440: 0x31323334
(gdb) x/w buffer
0x7fffffffe444: 0x34333231
为什么在内存中存储int和char数组数据有区别? 看起来值变量存储为大端,我是否遗漏了什么?
变量buffer
就是四个字节
+------+------+------+------+ | 0x31 | 0x32 | 0x33 | 0x34 | +------+------+------+------+ ^ ^ | | Low address High address
变量value
就是四个字节
+------+------+------+------+ | 0x34 | 0x33 | 0x32 | 0x31 | +------+------+------+------+ ^ ^ | | Low address High address
并且 x
命令以本机字节顺序显示(在您的情况下为小端),导致您获得输出。