In the previous article, the array in C + + also has the difference between static allocation and dynamic allocation in memory. A static array is created in a a[], which allocates space on the stack, and is dynamically allocated on the heap using New,malloc.
Arrays are either created in a static store (such as a global array) or created on a stack or heap. The array name corresponds to (instead of pointing to) a piece of memory whose address and capacity remain constant over the lifetime, and only the contents of the array can be changed. Look at the following example:
1#include <iostream>2 using namespacestd;3 voidTest ()4 {5 Charch[]="Hello";6ch[0]='H';7 Char*p=" World";8p[0]='W';//Error9cout<<ch<<Endl;Tencout<<p<<Endl; One } A intMain () - { - test (); the return 0; -}
The program uses the pointer to a constant string "world", the C + + constant string exists in the constant store, and cannot be modified, so there will be an error.
The array is allocated on the stack, or the difference between the allocations on the heap can be seen in the following example: Modify the test and main functions to the next
1 Char*Test ()2 {3 Charch[]="Hello";//On the Stack4 /*char* ch= New char[6];//on the heap5 ch[0]= ' h ';6 ch[1]= ' e ';7 ch[2]= ' l ';8 ch[3]= ' l ';9 ch[4]= ' 0 ';Ten ch[5]= ' + '; One */ A returnch; - } - intMain () the { - Char*p=test (); -cout<<p<<Endl; - return 0; +}
It is obvious that the program compiles: Warning c4172:returning address of local variable or temporary. The array allocated on the stack after the test call ends has been destroyed, and P is pointing to the invalid content for the wild pointer. This takes the array name as the return value of the L function. If the code in the following comment is allocated on the heap then there is no problem, note that the last ' \ ', the end of the string is ' s ' to determine the ending, otherwise it will be out of bounds output invalid content. Here you can see that the C + + array is stored in memory in the same way as in the previous section. Change the test to read as follows:
1 Char*Test ()2 {3 Char* ch=New Char[6];//On the heap4ch[0]='h';5ch[1]='e';6ch[2]='L';7ch[3]='L';8ch[4]='0';9ch[5]=' /'; Ten CharC[] ="Hello World"; One Char*p =C; Acout<<sizeof(c) << Endl;//12 bytes -cout<<sizeof(CH) << Endl;//4 bytes -cout<<sizeof(p) << Endl;//4 bytes the returnch; -}
Static array name with sizeof you can know the actual memory size of the array, while the pointer to the array takes up space that is the size of the normal pointer. When an array is passed as an argument to a function, the array is automatically degraded to a pointer of the same type.
In the previous article on the introduction of classes in memory allocation in C + + found a problem, when two pointers to the same object, found that delete a pointer to destroy the object, with another pointer to throw can call the class function, this is the wild pointer should be wrong AH. Look at the following example:
1#include <iostream>2 using namespacestd;3 classA4 {5 Public:6 voidFun ()7 {8cout<<"class of A:"<<Endl;9 }Ten intnum; One }; AA *Test () - { -A *p; the A; -a.num= One; -p=&A; -P->Fun (); + returnp; - } + intMain () A { ata*s=test (); -S->Fun (); - return 0; -}
Program Run Result:
That is, two times the output class of a, once in the test function, once is the s call. The A in test is allocated on the stack, the function should be destroyed after the end, why S can also call fun. The member data and functions in the original class are stored in different locations. The methods of the C + + class are stored in the program code area, and the data members (object data members) in the class are stored in the instance object of the class, that is, the member data is stored in the heap or stack. The member data for s points to the object has been destroyed, and the method of the class is still in.
To change line 8th of the above code to:cout<< "Class of A:" <<this->num<<endl; program run result is:
That is, the data member num for the output of S is invalid because the object has been disposed. There is a lot more to be learned about C + + memory management.
Allocation of C + + arrays in memory