Array is not pointer
|
|
int array[3];
array[2]=666;
|
int var;
var=66;
|
int *ptr = new int[3];
ptr[2] = 66;
|
这和第一段代码很像,但对编译器来说却意味着不同的意思。在第一段代码中第二条语句中,编译器生成的代码将做以下事情:
1) 指针下移两个位置并将其所指的值设为666
#include<iostream>
main(){
int int_input;
cin>>int_input;
cout<<(int_input + 4)<<endl;
return 0;
}
|
#include<iostream>
main(){
int *int_ptr = new int[1];
cin>>*int_ptr;
cout<< (*int_ptr + 4)<<endl;
delete(int_ptr);
return 0;
}
|
2212: main(){
00401000 push ebp
00401001 mov ebp,esp
00401003 sub esp,44h
00401006 push ebx
00401007 push esi
00401008 push edi
2213: int int_input;
2214: cin>>int_input;
00401009 lea eax,[ebp-4]
0040100C push eax
0040100D mov ecx,offset cin (00414c58)
00401012 call istream::operator>> (0040b7c0)
2215: cout<<(int_input+4)<<endl;
00401017 push offset endl (00401070)
0040101C mov ecx,dword ptr [ebp-4]
0040101F add ecx,4
00401022 push ecx
00401023 mov ecx,offset cout (00414c18)
00401028 call ostream::operator<< (0040b3e0)
0040102D mov ecx,eax
0040102F call ostream::operator<< (00401040)
2216: return 0;
00401034 xor eax,eax
2217: }
|
2212: main(){
00401000 push ebp
00401001 mov ebp,esp
00401003 sub esp,4Ch
00401006 push ebx
00401007 push esi
00401008 push edi
2213: int *int_ptr = new int[1];
00401009 push 4
0040100B call operator new (004011b0)
00401010 add esp,4
00401013 mov dword ptr [ebp-8],eax
00401016 mov eax,dword ptr [ebp-8]
00401019 mov dword ptr [ebp-4],eax
2214: cin>>*int_ptr;
0040101C mov ecx,dword ptr [ebp-4]
0040101F push ecx
00401020 mov ecx,offset cin (00414c38)
00401025 call istream::operator>> (0040b8a0)
2215: cout<< (*int_ptr + 4)<<endl;
0040102A push offset endl (004010a0)
0040102F mov edx,dword ptr [ebp-4]
00401032 mov eax,dword ptr [edx]
00401034 add eax,4
00401037 push eax
00401038 mov ecx,offset cout (00414bf8)
0040103D call ostream::operator<< (0040b4c0)
00401042 mov ecx,eax
00401044 call ostream::operator<< (00401070)
2216: delete(int_ptr);
00401049 mov ecx,dword ptr [ebp-4]
0040104C mov dword ptr [ebp-0Ch],ecx
0040104F mov edx,dword ptr [ebp-0Ch]
00401052 push edx
00401053 call operator delete (00401120)
00401058 add esp,4
2217: return 0;
0040105B xor eax,eax
2218: }
|
Array is not pointer |
|
With many thanks for these useful tutorials, I felt it's necessary to send this text about pointers and arrays. Unfortunately pulling out something wrong that is put in humans head is a bit difficult. So understanding the things correct and precise is very important to avoid further misconceptions.
An array is not equal to a pointer. It is a sequence of simple variables in memory.
When we write
int array[3]; array[2]=666; |
C/C++ compiler doesn't see array[0] as an address to an integer value, it takes it directly as a value, exactly as same as writing
int var; var=66; |
That's obvious that "var" is not a pointer exactly as array[2] is not.
But if we use a pointer instead of an array, the face of the code is the same but compiler generates different assembly code. For example
int *ptr = new int[3]; ptr[2] = 66; |
Is similar to the first code but not with the same meaning to the compiler. In the first code (second line), compiler generates code that will do the following:
1) Go two places next of the array[0] and make it equal to 666.
But in code with pointer it is:
1) Fetch the value (address) of the ptr[0].
2) Add two to it.
3) Make the value pointed by it to 66.
Actually the value of "array", "&array" and "&array[0]" is equal. But the type of "&array" is different (a memory address to an array not an array member).
Here is another example to make the article more understanding. I want to write a program that gets an integer from user, adds 4 to it and then prints out the result. Once I write it using an integer pointer and once with an integer variable.
With integer it will be:
#include<iostream> main(){ int int_input; cin>>int_input; cout<<(int_input + 4)<<endl; return 0; } |
And using a pointer it will be:
#include<iostream> main(){ int *int_ptr = new int[1]; cin>>*int_ptr; cout<< (*int_ptr + 4)<<endl; delete(int_ptr); return 0; } |
Who thinks these programs are exactly the same?
Lets take a look at their assembly. For the first code with an integer it is:
2212: main(){ 00401000 push ebp 00401001 mov ebp,esp 00401003 sub esp,44h 00401006 push ebx 00401007 push esi 00401008 push edi 2213: int int_input; 2214: cin>>int_input; 00401009 lea eax,[ebp-4] 0040100D mov ecx,offset cin ( 00401012 call istream::operator>> (0040b 2215: cout<<(int_input+4)<<endl; 00401017 push offset endl (00401070) 00401022 push ecx 00401023 mov ecx,offset cout ( 00401028 call ostream::operator<< (0040b3e0) 0040102D mov ecx,eax 2216: return 0; 00401034 xor eax,eax 2217: } |
And for the code with pointer it is:
2212: main(){ 00401000 push ebp 00401001 mov ebp,esp 00401003 sub esp,4Ch 00401006 push ebx 00401007 push esi
|