问题描述
要澄清的问题,请遵守C / C ++ code片段:
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!
所以,如果点== 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?
推荐答案
标准C ++在部分国家的 8.3.4数组的 的数组类型的对象包含一个连续分配的非空集类型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."
这是为什么, []数组
将是一组连续的 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".
这样你就假设这些对象的contiguousness一切都将是不确定的行为,不便于携带。你甚至不能肯定在其中创建这些对象的地址的顺序。
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.
现在我已经打了你的code的修改版本:
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".
在运行此code我得到:
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.
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.
这篇关于为什么连续int数据类型varaiables位于12字节在Visual Studio中的偏移?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!