Blog Reprint Please specify the original address: http://blog.csdn.net/sunliymonkey/article/details/48139183
Question: why c[5] = = 5[c] in the C + + language?
This question, originally in the German question saw, at first did not know its mechanism, speculation and C language of the compilation mechanism, so through disassembly, speculation, verification, finally found the original.
Here is the procedure for me to analyze the problem, and first look at the code for the array:
#include <iostream> using namespace STD ; Span class= "Hljs-keyword" >int Main () {int a[5 ]; for (int i = 1 ; i < 5 ; i++) {A[i] = i + 5 ; } a[3 ] = 11 ; 3 [A] = 15 ; cout << a[3 ] << Endl; System ( "pause" ); return 0 ;}
To disassemble code by using Microsoft Visual 2010:
7: { 8: a[i] = i + 5;00251B80 8B 45 D8 mov [i] 00251B83 83 C0 05 add eax,5 // eax = i + 500251B86 8B 4D D8 mov [i] // 用ecx存放a[i]下标i的值00251B89 89 44 8D E4 mov [ebp+ecx*4-1Ch]// 将eax的值传给a[i] 9: }
Based on the assembly code above, we can find the compiler a[i]
's representation of the Assembly:
a[i]: dword ptr [ebp+ecx*4-1Ch]
Where the expression is a dword
double word (four bytes) ptr
, a pointer abbreviation that represents the pointer, and [addr]
a representation of the addr
address location in. The entire equation represents the value of a variable of 4 bytes from the address at which it is located, and, in ebp+ecx*4-1Ch
a[i]
contrast, can be guessed:
a[i] <---> [ebp+ecx*4-1Ch] a <---> ebp - 1Ch //数组首地址 i <---> ecx //数组下标 int <---> 4 //变量大小
By comparing a
the values with Microsoft Visual 2010 ebp-1Ch
, you can see that they are equal, proving our correspondence above, and summarizing the compiler's interpretation of the array:
ptr [数组首地址 + 数组下标 * sizeof(变量类型)]
It seems that this conclusion is more obvious, but if you follow this conclusion:
A[3] translated as: ptr [a + 3 * sizeof (int)]
3[a] translated as: ptr [3 + A * sizeof (int)]
In this case, the two should not be the same, but the actual running program finds that the two are indeed equal. This makes us feel that the compiler is very smart and can identify the a
real array header, which 3
is the real array subscript. Given that the compiler's talent is given by our programmers, it is clear that it does not really identify who is the first address of the array, but is obtained through preset guidelines.
Here, 3
with a
, for the compiler, the former is a constant, and the latter is a pointer variable. From a higher level a[i]
[i]a
, both are variables, but one is an integer type and the other is a pointer type. It is possible to guess that the compiler should recognize the pointer variable as an array header, while the remaining variable values are labeled as arrays .
Next we do the following experiments to verify:
inta[5];int*p = A;inti =2, B = &a;//a: Array First addressI[a] =1;//Correct 2[A] =1;//Correct(i+2) [A +1]=1;//Correct //p:int pointer VariableI[P] =1;//Correct 2[P] =1;//Correct(i+2) [p+1]=1;//Correct //No pointer variableb[2] =3;//Failure error C2109: Subscript requires array or pointer type 2[B] =4;//Failure error C2109: Subscript requires array or pointer typeI[B] =5;//Failure error C2109: Subscript requires array or pointer typeB[i] =6;//Failure error C2109: Subscript requires array or pointer type
As a result, the compiler must have a pointer variable as the base address for array processing, and other values as an array subscript . You may be tempted to ask: what if there are two pointer variables? According to the above speculation, there are two pointer variables, the compiler seems to be unable to process, the experimental validation also shows that there are multiple pointer variables, will be error:
int a[5],b[5]; a[b+24; // 失败 error C2107: 非法索引,不允许间接寻址
At this point the problem analysis is complete, summarized as follows:
A[i] As the meaning of I[a]
Array A[i] is interpreted as: PTR [array first address + subscript * sizeof (variable type)]
A pointer variable must appear in the expression, regardless of its position, as the first address of the array, and the remaining value as the array subscript
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced. Blog site: Http://blog.csdn.net/sunliymonkey
Why c[5] = = 5[c]