catalogue
Memory distribution in C
Memory space is divided into heap, stack and program area
Stacking area:
that is, dynamic memory allocation. Programmers can apply for any size of space through malloc, but remember to release free after using this space, so as not to cause memory leakage.
Stack area:
the storage units of local variables in the function are created on the stack. When the function is executed, these storage units will be released.
The program area includes:
bss/ZI segment - > zero segment (uninitialized global variables are placed here)
RW segment - > initialized global and local variables (can be modified)
RO segment - > read only variable (constant)
Example: char * str = "hello"; (it cannot be modified here.).
Difference between heap area and stack area
The difference between the two is mainly the difference of memory release, which is shown in the following code:
Stack area:
#include <iostream.h> #include <string.h> #include <stdlib.h> char* test() { char c[3] = {'a','b','c'}; //This is a local variable in the stack area cout<<"Here is test Inside the function"<<endl; cout<<"c[0]"<<c[0]<<"c[1]"<<c[1]<<"c[2]"<<c[2]<<endl; return c; } int main() { char *s = test(); cout<<"s[0]"<<s[0]<<"s[1]"<<s[1]<<"s[2]"<<s[2]<<endl; return 0; }
result:
from the results, we can see that in the test function, the array c can be output normally, but it is strange to return the address to s for output. This is because at the time of return, the test function ends, the address of c will be recycled, and the inner array will naturally disappear.
Stacking area:
#include <iostream.h> #include <string.h> #include <stdlib.h> char* test() { char *cc=(char*)malloc(3); //Open up space in the reactor area char c[3] = {'a','b','c'}; //This is a local variable in the stack area strcpy(cc,c); cout<<"Here is test Inside the function"<<endl; cout<<cc[0]<<cc[1]<<cc[2]<<endl<<endl; return cc; //Returns the address of the heap } int main() { char *ss=test(); cout<<"Here is main Inside the function"<<endl; cout<<ss[0]<<ss[1]<<ss[2]<<endl<<endl; return 0; }
result:
first of all, this method is not recommended here, because it can be found that the cc in test opens up space through malloc, but it is not released, which will cause memory leakage. However, we can see that the address of the heap area is not recycled after the function is completed, and the contents can still be obtained.
Correct writing of stacking area:
#include <iostream.h> #include <string.h> #include <stdlib.h> void test(char *SS) { char c[3] = {'a','b','c'}; //This is a local variable in the stack area memcpy(SS,c,3); cout<<"Here is test Inside the function"<<endl; cout<<c[0]<<c[1]<<c[2]<<endl<<endl; } int main() { char*ss=(char *)malloc(3); test(ss); cout<<ss[0]<<ss[1]<<ss[2]<<endl<<endl; free(ss); return 0; }
RO constant area assignment error (pointer assignment error)
We may write this when we can only assign values
void test_fun(char *str) { strcpy(str,"okk"); printf("str=%s\n",str); } int main() { char *p ="hello"; //Stack area test_fun(p); return 0; }
result:
as a result, it should be reported as wrong. Why is RO readonly read-only? You can't correct it.
Interpretation:
&P this is the address of the pointer. You can see that the address of the pointer is 0019FF2C
Is the memory in it 0C 91 42 00 70? Because the pointer is 4 bytes, we can see the first four bytes, and the address is read in reverse, so the content of this piece is
00 42 91 0C what is this? This is the address pointed to by the p pointer, that is, the address of the "hello" string.
Then we enter the address 00 42 91 0C, which is the address of our hello string. This is the RO area and cannot be modified.
Figure:
Similarly, static is the same, and the assignment will report an error
int main() { const int i = 0; //static state i = 2; return 0; }
report errors: