问题描述
为阐明问题,请观察c/c ++代码片段:
To clarify the question, please observe the c/c++ code fragment:
int a = 10, b = 20, c = 30, d = 40; //consecutive 4 int data values.
int* p = &d; //address of variable d.
现在,在Visual Studio(2013年测试)中,如果p的值== hex_value(可以在调试器的内存窗口中查看),则可以观察到其他变量a,b,c,和d分别相差12个字节!
Now, in visual studio (tested on 2013), if value of p == hex_value (which can be viewed in debugger memory window), then, you can observe that, the addresses for other variables a, b, c, and d are each at a 12 byte difference!
因此,如果为p == hex_value
,则遵循以下条件:
So, if p == hex_value
, then it follows:
&c == hex_value + 0xC
(请注意十六进制C是十进制的12)
&c == hex_value + 0xC
(note hex C is 12 in decimal)
&b == &c + 0xC
&a == &b + 0xC
那么,为什么会有12个字节的偏移量而不是4个字节-int只有4个字节?
So, why is there a 12 bytes offset instead of 4 bytes -- int are just 4 bytes?
现在,如果我们声明了一个数组:
Now, if we declared an array:
int array[] = {10,20,30,40};
值10、20、30、40分别位于4个字节的差异上!
The values 10, 20, 30, 40 each are located at 4 bytes difference as expected!
任何人都可以解释这种行为吗?
Can anyone please explain this behavior?
推荐答案
8.3.4数组部分中的标准C ++声明,数组类型的对象包含连续分配的非T类型的N个子对象的空集合."
The standard C++ states in section 8.3.4 Arrays that "An object of array type contains a contiguously allocated non-empty set of N subobjects of type T."
这就是为什么array[]
将是一组连续的int
的原因,并且一个元素和另一个元素之间的差异将恰好是sizeof(int).
This is why, array[]
will be a set of contiguous int
, and that difference between one element and the next will be exactly sizeof(int).
对于局部/块变量(自动存储),未提供任何此类保证.唯一的语句在 1.7节中. C ++内存模型:每个字节都有一个唯一的地址." 和 1.8. C ++对象模型:该对象的地址是它占据的第一个字节的地址.两个对象(...)应该具有不同的地址" .
For local/block variables (automatic storage), no such guarantee is given. The only statements are in section 1.7. The C++ memory model: "Every byte has a unique address." and 1.8. The C++ object model: "the address of that object is the address of the first byte it occupies. Two objects (...) shall have distinct addresses".
因此,假设此类对象是连续的,您所做的一切都是不确定的行为,并且是不可移植的.您甚至不能确定在这些对象中创建地址的顺序.
So everything that you do assuming contiguousness of such objects would be undefined behavior and non portable. You cannot even be sure of the order of the addresses in which these objects are created.
现在,我使用了修改后的代码:
Now I have played with a modified version of your code:
int a = 10, b = 20, c = 30, d = 40; //consecutive 4 int data values.
int* p = &d; //address of variable d.
int array[] = { 10, 20, 30, 40 };
char *pa = reinterpret_cast<char*>(&a),
*pb = reinterpret_cast<char*>(&b),
*pc = reinterpret_cast<char*>(&c),
*pd = reinterpret_cast<char*>(&d);
cout << "sizeof(int)=" << sizeof(int) << "\n &a=" << &a << \
" +" << pa - pb << "char\n &b=" << &b << \
" +" << pb - pc << "char\n &c=" << &c << \
" +" << pc - pd << "char\n &d=" << &d;
memset(&d, 0, (&a - &d)*sizeof(int));
// ATTENTION: undefined behaviour:
// will trigger core dump on leaving
// "Runtime check #2, stack arround the variable b was corrupted".
运行此代码时,我得到:
When running this code I get:
debug release comment on release
sizeof(int)=4 sizeof(int)=4
&a=0052F884 +12char &a=009EF9AC +4char
&b=0052F878 +12char &b=009EF9A8 +-8char // is before a
&c=0052F86C +12char &c=009EF9B0 +12char // is just after a !!
&d=0052F860 &d=009EF9A4
因此,您看到甚至在同一编译器上,地址的顺序甚至可能会更改,具体取决于构建选项!实际上,在发布模式下,变量是连续的,但顺序不同.
So you see that the order of the addresses may even be altered on the same compiler, depending on the build options !! In fact, in release mode the variables are contiguous but not in the same order.
调试版本上的多余空格来自选项 /RTCs .我故意用一个苛刻的memset()假定变量是连续的来覆盖它们.执行退出时,我立即收到一条消息:运行时检查#2,围绕变量b的堆栈已损坏" ,这清楚地说明了这些额外字符的用途.
如果删除该选项,则将获得MSVC13连续变量,每个变量都与您期望的一样为4个字节.但是,也不会再有关于堆栈损坏的错误消息.
The extra spaces on the debug version come from option /RTCs. I have on purpose overwritten the variables with a harsh memset() that assumes they are contiguous. Upon exit of the execution, I get immediately a message: "Runtime check #2, stack arround the variable b was corrupted", which clearly demonstrate the purpose of these extra chars.
If you remove the option, you will get with MSVC13 contiguous variables, each of 4 bytes as you did expect. But there will be no more error message about corruption of stack either.
这篇关于为什么在Visual Studio中,连续的int数据类型变量位于偏移12个字节的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!