First define the header file
#include<stdio.h> #include<stdlib.h> #include<errno.h> #include<string.h>
1. Allocate memory space function malloc()
Call form:
(type specifier *) malloc (unsigned int size)
Function:
Allocate a continuous area with a length of 'size' bytes in the dynamic storage area of memory
int *p = (int *)malloc(10*sizeof(int)); //important no initialization
Since malloc is of void type, it is forced to convert the type (type specifier) into int type and allocate a length of 10 size bytes
if(p==NULL) { printf("%s",strerror(errno)); }
If the development fails and a null pointer is returned, the strerror function can be used to prompt relevant errors (compared with return 0, the problem of the code can be seen more clearly); At the same time, you need to define the third of the header file, otherwise you can't use this function. If the development is successful, the data can be stored.
else { int i = 0; for(i=0;i<10;i++) { *(p+i) = i; } for(i=0;i<10;i++) printf("%d",*(p+i)); }
Store the corresponding elements in the opened space. Finally, the space just applied must be released. If it is not released, it will cause serious memory leakage. At the same time, in order to prevent damaging the created array with the help of free, p will generally be pointed to the empty pointer
free(p); p = NULL;
The complete code is as follows:
#include<stdio.h> #include<stdlib.h> #include<errno.h> #include<string.h> int main() { int *p = (int *)malloc(10*sizeof(int)); //important no initialization if(p==NULL) { printf("%s",strerror(errno)); } else { int i = 0; for(i=0;i<10;i++) { *(p+i) = i; } for(i=0;i<10;i++) printf("%d",*(p+i)); } free(p); p = NULL; }
2. Allocate memory space function calloc ()
Call form:
(type specifier *) calloc (n,size)
Function:
Allocate n consecutive areas with a length of 'size' bytes ()
int *p = (int*)calloc(10,sizeof(int)); //Each byte of initialization is 0, which is inefficient, but initialization
The function calloc initializes each byte of the requested space to 0 before the address (malloc will not initialize and will directly start storing data)
The rest is as like as two peas malloc functions.
The code snippet is as follows:
#include<stdio.h> #include<stdlib.h> #include<errno.h> #include<string.h> int main() { int *p = (int*)calloc(10,sizeof(int)); if(p==NULL) { printf("%s",strerror(errno)); } else { int i =0; for(i=0;i<10;i++) *(p+i) = i ; for(i=0;i<10;i++) printf("%d",*(p+i)); free(p); p = NULL; } }
3.realloc
This function is used to dynamically adjust the opened memory.
The calling format is:
int *ptr = realloc(p,40);
Take a simple example:
We are using malloc to open up 20 byte space, but the 20 byte space can not meet our use. We hope to have 40 byte space. Here, we can use realloc to dynamically adjust the memory space
Precautions for using relloc:
1. If there is enough space to add after the space pointed to by p, add it directly and return to p
2. If there is not enough memory to add after the space pointed to by p, the realloc function will find a new memory area, open up a space to meet the needs, copy back the data in the original memory, release the old space, and finally return the opened memory space address
The code snippet is as follows:
#include<stdio.h> #include<stdlib.h> #include<errno.h> #include<string.h> int main() { int *p = (int *)malloc(5*sizeof(int)); if(p==NULL) { printf("%s",strerror(errno)); } else { int i = 0; for(i=0;i<5;i++) { *(p+i) = i; } int *ptr = realloc(p,40); if(ptr!=NULL) //Judge whether the new pointer is a null pointer (judge whether the development is successful) { p = ptr; } for(i=5;i<10;i++) { *(p+i) = i; } for(i=0;i<10;i++) { printf("%d ",*(p+i)); } free(p); p=NULL; } }
4. Free space function (free)
Call form:
free(void *ptr);
Function: release a memory space pointed to by ptr. ptr is a variable of any type pointer, which points to the first address of the released area.
Creation and use of one-dimensional dynamic array:
#include<stdio.h> #include<malloc.h> #include<stdlib.h> #include<errno.h> void main() { int *p = NULL,num,i; printf("please input the number of element: "); scanf("%d",&num); p = (int *)malloc(sizeof(int)*num); if(p == NULL) { printf("%s",strerror(errno)); //Print error } else { printf("please input %d elements:",num); for(i=0;i<num;i++) scanf("%d",&p[i]); printf("%d elements are : \n",num); for(i=0;i<num;i++) printf("%d ",p[i]); free(p); //Free the memory block requested by malloc() function p = NULL; } }
Creation and use of 2D arrays:
#include<stdio.h> #include<malloc.h> #include<stdlib.h> #include<errno.h> void main() { int n1,n2; int **p,i,j; printf("Please enter one-dimensional length:"); scanf("%d",&n1); puts("Please enter 2D length:"); scanf("%d",&n2); //Follow the principle of from the inside out p = (int**)malloc(n1*sizeof(int *)); if(p == NULL) { printf("%s",strerror(errno)); //Print error } else { for(i=0;i<n1;i++) { p[i] = (int*)malloc(n2*sizeof(int)); if(p[i] == NULL) { printf("%s",strerror(errno)); //Print error } for(j=0;j<n2;j++) { p[i][j] = i+j+1; printf("%d\t",p[i][j]); } puts(""); } for(i=0;i<n1;i++) free(p[i]); //Release 2D pointer free(p); //Release one-dimensional pointer } }