1 the necessary knowledge
Address: Only variables have the address, constant, no address, in addition to the definition of const pseudo constants.
Pointer (TYPE *): Any data type can be defined pointer, a pointer to a data type itself. Since the pointers are stored in the address (32-bit operating system, 32-bit address), so no matter what type of pointer occupies 4 bytes of space.
Reference (TYPE &): can not be defined in C ++ alone, will define the initialization, a variable is an alias.
2. pointer works
Pointer addressing 2.1
Because the pointer saves only the first address, using the modified pointer type modifier (TYPE *), be able to interpret the data type this address, different data types, the space occupied different.
For example analysis:
1 #include <iostream> 2 3 #pragma warning(disable:4996) 4 5 using namespace std; 6 7 int main() 8 { 9 int Value = 0x12345678; 10 int* iptr = &Value; 11 char* cptr = (char*)& Value; 12 short* sptr = (short*)& Value; 13 14 printf("%08x\n", *iptr); 15 printf("%08x\n", *cptr); 16 printf("%08x\n", *sptr); 17 18 system("PAUSE"); 19 return 0; 20 }
Data stored in the memory is "78563412", "78" head address,
Iptr pointer to an int pointer to an int (size 4 bytes) of the address interpretation so as to remove the small end data, "12345678." Pointer cptr similar principle, 1-byte data taken out, "78." Sptr pointer extracted two bytes of data, "5678."
Similarly, the work can be inferred pointer addition.
for example
1 #include <iostream> 2 3 #pragma warning(disable:4996) 4 5 using namespace std; 6 7 int main() 8 { 9 int Value = 0x12345678; 10 int* iptr = &Value; 11 char* cptr = (char*)& Value; 12 short* sptr = (short*)& Value; 13 14 printf("%08x\n", *iptr); 15 printf("%08x\n", *cptr); 16 printf("%08x\n", *sptr); 17 printf("\n"); 18 iptr += 1; 19 cptr += 1; 20 sptr += 1; 21 printf("%08x\n", *iptr); 22 printf("%08x\n", *cptr); 23 printf("%08x\n", *sptr); 24 25 system("PAUSE"); 26 return 0; 27 }
iptr += 1; 00735096 add eax,4 00735099 mov dword ptr [ebp-18h],eax cptr += 1; 0073509C mov eax,dword ptr [ebp-24h] 0073509F add eax,1 007350A2 mov dword ptr [ebp-24h],eax sptr += 1; 007350A5 mov eax,dword ptr [ebp-30h] 007350A8 add eax,2 007350AB mov dword ptr [ebp-30h],eax
As can be seen by the assembler code, the pointer is interpreted according to the pointer adder type.
iptr + = 1, the 4-byte goes back, and then the address, output data type int explained.
cptr + =, goes back 1 byte (the first address becomes "56"), and then to explain the address char type, 1-byte data fetch outputs "56."
sptr similar ... ...
3. references
C ++ references are actually pointers to simplify the operation of the pointer package, allowing users to see the stored memory address space.
for example
int main() { int num = 0x12345678; int* iptr = # int& iref = num; return 0; }
Assembly code
int main() { 00C016F0 push ebp 00C016F1 mov ebp,esp 00C016F3 sub esp,0E8h 00C016F9 push ebx 00C016FA push esi 00C016FB push edi 00C016FC lea edi,[ebp+FFFFFF18h] 00C01702 mov ecx,3Ah 00C01707 mov eax,0CCCCCCCCh 00C0170C rep stos dword ptr es:[edi] 00C0170E mov eax,dword ptr ds:[00C0A004h] 00C01713 xor eax,ebp 00C01715 mov dword ptr [ebp-4],eax 00C01718 mov ecx,0C0C000h 00C0171D call 00C011FE int num = 0x12345678; 00C01722 mov dword ptr [ebp-0Ch],12345678h int* iptr = # 00C01729 lea eax,[ebp-0Ch] 00C0172C mov dword ptr [ebp-18h],eax int& iref = num; 00C0172F lea eax,[ebp-0Ch] 00C01732 mov dword ptr [ebp-24h],eax return 0; 00C01735 xor eax,eax }
4. Constant
Constant data before running the program has existed. Const can be defined using the #define or constants.
#define is a real constant, and const is a constant realization of judgment by the compiler, is a fake constant. When the compiler, it was discovered const variable defined in a constant value, the program will replace all of the variable to a constant value.
for example
#include <iostream> using namespace std; int main() { const int num = 0x12345678; int* ptr = (int*)& num; *ptr = 0x2; int count = num; printf("%08x\n", num); printf("%08x\n", *ptr); system("PAUSE"); return 0; }
Since the compiler replaces num advance, so we change the constant value output num is still value before the change, and pointed to by ptr is now constant values in memory.