Memory Management process Space
SOURCE Program: Source code
Program (executable file): Files compiled by the source program
Process: The concept of time, the executable file is pulled up to the end of this process. Processes can be pulled up multiple.
Process space: The executable file is pulled up and distributed in memory.
The distribution of process space:
stackheapdata>未初始化>初始化text
32-bit machine maximum addressing 3G,
Focus Stack, heap
Stacks: auto-modified variables, who tune who use, run out to eliminate. No manual intervention is required. (The relationship between the stack and the stack), stack overflow. Mainly used for the exchange of data, for applications not suitable for large space (large data application heap)
。 The size does not exceed 10M, avoiding a large number of recursion.
Direction of development: from high to low first defined in the high, after the Declaration on the low.
Heap: You can store any type of data type, but you need to apply and release it yourself.
Application: malloc ();
Release: Free ();
Size: Large space.
Direction of development: from low to high
char * p = (char*)molloc(1024*1024*1024);//加内存可解决strcpy(p,"adbksj");printf("sjdkjfk");free(p);
Request Mallloc in byte size
Basic types of applications
int * p = (int*)malloc(1*sizeof(int));//申请int类型的4个字节的大小100;//初始化printf("*p = %d\n",*p);//100
Constructed type--array of applications:
The smallest unit of application and initialization is byte
memset (p,0,sizeof (int)) initialization
int arr[10];int *p = (int*)malloc(10*sizeof(int));memset(p,0,10*sizeof(int));//初始化,p:首地址,0:初始化元素,后面是大小for(int010;i++) { printf("%d\n",p[i]);//不知道的值,不能使用*p++}free(p);
Calloc ()
int *p = (int *)calloc(10,sizeof(int));//10个4个单元的大小for(int010;i++) { printf("%d\n",p[i]);//自动初始化了}
ReAlloc () expansion
char * pa = (char*)malloc(10);strcpy(pa,"1234567890abcdef");//实际数据大于了申请的空间,这时候使用realloc();
char *pa = (char*)malloc(10);char *newPa;newPa = realloc(pa,20);strcpy(newPa,"1234567890abcdef");//newPa与pa有时候会不一样free(newPa);
Or
char *pa = (char*)malloc(10);pa = realloc(pa,20);strcpy(pa,"1234567890abcdef");//newPa与pa有时候会不一样free(pa);
ReAlloc () There are two situations, one is the lack of space after you continue to expand, the other is to open up the space shortage, re-open enough space to copy the data past.
Application Model
#include <stdio.h>#include <stdlib.h>intMain () {int*PA;intLen printf"Please new len:"); scanf"%d", &len); PA = (int*) malloc (len*sizeof(int));intOldlen = Len; for(inti =0; i<len;i++) {Pa[i] = -+i; printf"%d\ n", Pa[i]); } printf ("Please large len:"); scanf"%d", &len); PA = (int*) ReAlloc (pa,len*sizeof(int)); for(inti =0; i<len;i++) {if(Oldlen <= i) pa[i] = $+i; printf"%d\ n", Pa[i]); } printf ("Please small large len:"); scanf"%d", &len); PA = (int*) ReAlloc (pa,len*sizeof(int)); for(inti =0; i<len;i++) {printf ("%d\ n", Pa[i]); } free (PA);return 0;}
FAQ Resolution
Error model one: In the server model, commonly used in the large cycle, in the cycle did not release the original space, re-apply for new space, causing the original space memory leaks.
while(1) {Char*pa = (Char*) malloc ( -); printf"Ooooooooooooo\ n"); printf"Ooooooooooooo\ n"); printf"Ooooooooooooo\ n"); printf"Ooooooooooooo\ n"); printf"Ooooooooooooo\ n"); printf"Ooooooooooooo\ n"); printf"Ooooooooooooo\ n"); printf"Ooooooooooooo\ n"); printf"Ooooooooooooo\ n"); printf"Ooooooooooooo\ n"); printf"Ooooooooooooo\ n"); printf"Ooooooooooooo\ n"); printf"Ooooooooooooo\ n"); printf"Ooooooooooooo\ n"); PA = (Char*) malloc ( -);//No release after previous application, re-application //First application, no release, space in memory, second application to the same variable, last space not released. Also occupies memory space. Release to pair with. Two times use free (), and will hang up the machine. malloc extra free will hang up, and free will hang up for malloc. }
Empty and empty
Logic for memory use: Apply, empty, use, release, common error: After releasing empty null to empty again to make use of, or release after the use of the Philippine side.
char *pc = (char*)malloc(100);//申请if(pc == NULL) { printf("error\n");//内存里已经没有空间了 exit(-1);}strcpy(px,"afakjfklajflkfja");//使用free(pc);//释放pc = NULL;//置空
Who applies for release and prevents multiple releases
void func(char*p) { printf("%s\n",p); free(p); p = NULL;}int main() { char *p = (char*)malloc(100); if(NULL == p) { exit(-1); } strcpy(p,"adajsfkljlk"); func(p); free(p); p = NULL;//会挂机,func中无需释放,谁申请谁释放 return0;}
Open address space
void foo() { printf("p = %s\n",p);}void func() { printf("p = %s\n",p);}int main() { char *p = (char*)malloc(100); strcpy(p,"afkajfklj"); func(p); foo(p); return0;}//p属于同一个变量,但是属于不同空间。p地址在不同的作用域中是共同开放的。
Return of heap and stack space
Stack space cannot be returned, and heap space can be returned.
1. can only return
2. Address can also return, return to the address do not go to use
3. Who uses who drives, runs out of the elimination, can not return to the----stack
4. The space on the heap can be returned
intFunc () {intA = -;returnA;}int* Foo () {intA = -;/*printf ("&a =%p\n", PA); not under Windows. */ int*PA = &a; printf"&a =%p\ n", PA);returnPA;}int*func2 () {//Who uses who drives, runs out, can not return to the----stack intarr[ -];returnarr;}intMain () {intA = func ();//Can returnprintf"A =%d\ n", a);int*PA = foo ();//address can be returned, the space disappears. printf"PA =%p\ n", PA);return 0;}
Return of the heap
Char* GETFORMATMEM (intSizeCharContent) {Char*p = (Char*) malloc (size*sizeof(Char));if(NULL = = p) Exit (-1); memset (p,content,size*sizeof(Char)-1); p[size*sizeof(Char)-1] = ' \0‘;returnP;}intMain () {Char*p = Getformatmem ( -, ' a ');//Packaged applicationprintf"p =%s\ n", p); Free (p);//release, return 0;}
C + + (memory management)