First, the malloc function
/*First you need to import the header file #include <stdlib.h> malloc void* malloc (n); n is the size of the byte to open up the heap space, the number of bytes opened up to n is the first address of the open space void* is a universal pointer, which means you can use any type of pointer to point*/ //malloc Usage int*p = malloc (sizeof(int)); *p = -; printf ("%p%d\n", p,*p);//0x100105490 Char*PC = malloc (sizeof(Char)*4); pc[0]='a'; pc[1] ='b'; pc[2] ='C'; pc[3] =' /'; printf ("%s\n", PC); Char*PC = malloc (4); *PC ='a'; * (pc+1) ='b'; * (pc+2) ='C'; * (pc+3) =' /'; printf ("%s\n", PC); Char*PC = malloc (4); Char*temp =pc; *PC ='a'; PC++;//Self +1*PC ='b'; PC++; *PC ='C'; PC++; *PC =' /'; printf ("%s\n", temp); Char*PC = malloc (1); *PC ='h'; printf ("%c\n", *pc);Second, calloc function
//calloc Usage intnums[3] = {Ten, -, -};//in the Stack area int*p = Calloc (4,3);//in the heap areap[0] =Ten; p[1] = -;p [2] = -; for(inti =0; i<3; i++) {printf ("%d\n", P[i]); } int*p = Calloc (4,3);//in the heap area* (P) =Ten; * (p+1) = -; * (p+2) = -; for(inti =0; i<3; i++) {printf ("%d\n", P[i]); }
The difference between malloc and calloc:
malloc will not initialize, there will be garbage values, Calloc will be initialized to 0
Char*PCH = malloc ( -); for(inti =0; i< -; i++) {printf ("%d", * (pch+i)); } printf ("\ n------------split line-------------\ n"); Char*PCH2 = Calloc (1, -); for(inti =0; i< -; i++) {printf ("%d", * (pch2+i)); }
Three, realloc function
/*When the space you manually opened up is not enough, this time we need to expand, we have to use realloc this function. Realloc:realloc (the first address of the space to be expanded, the size of the bytes after operation); Function: To expand or reduce the amount of space you have passed in, the size of the bytes after operation will be the following parameters. Note: It is best to use the pointer variable to receive the return value when using the ReAlloc function. Because you are expanding, it is likely that the system will help you in a new large enough space to expand, so that the first address of the space has changed. */ //Use of ReAlloc Char*p = malloc ( -); *p ="Mobile phone client or the space distribution of credit card payment how many parts of the technology equipment room number how much space is to show me a"; //so we need to re-accept it.p = realloc (P, +);//Zoom inp = realloc (P, -);//Zoom Out
Iv. memory leaks
/*memory leaks: Memory capacity is limited, and then if you do not reclaim the unused space in time, but always open up new space, then memory will not be enough, called memory leaks (like water cup filled with water). Code inside: A block of space has been occupied, not recycled is called memory leaks. Reclaim the heap space you have opened up: Free:free (the first address of the space to be recycled); Wild pointer: A pointer to a space that has been reclaimed is called a wild pointer. Pointing to unavailable memory space is called a wild pointer. Prevent wild pointers: Simply set the pointer variable to null immediately after recycling. */int*p = malloc (4);//Memory Leaks//memory leaks because the heap space that P points to is not recycled//then how to recycle the program itself?? Free (p);//at this point the first address of the space that P points to is recycled, when p becomes a wild pointerP= NULL;//when the space that the P points to is recycled, p is immediately null to prevent wild pointer errors
Basic knowledge of C language learning points (19): Memory manipulation functions