Some recent knowledge is shared ~ as follows:
- Stack area usually stores: local variables, parameters, and static space.
- The space requested by malloc, free, calloc and realloc is usually placed in the heap.
- Static declarations are usually placed in static storage.
The following describes several dynamic memory functions:
1.malloc function and free function (both need header files stdlib.h or malloc.h):
malloc function is to apply for a continuously available space from memory and return a pointer to this space.
- If the development is successful, a pointer to the developed space is returned.
- If the development fails, a NULL pointer is returned, so the return value of malloc must be checked.
- The type of the return value is void *, so the malloc function does not know the type of open space, which is determined by the user when using it.
- If the parameter size is 0, the behavior standard of malloc is undefined.
- The function prototype is void* malloc(size_t size);
free function:
- The function prototype is: void free(void* ptr);
- If the space pointed to by the parameter ptr is not dynamically opened up, the behavior of the free function is undefined.
- If the parameter ptr is a NULL pointer, the function does nothing.
For example:
#include<stdio.h> #include<stdlib.h> void main() { int n; printf("input n:>"); scanf("%d", &n); //Portability int *ar = (int*)malloc(sizeof(int) * n); if(ar == NULL) { printf("Out Of Memory.\n"); return; } for(int i=0; i<n; ++i) { ar[i] = i+1; } for(int i=0; i<n; ++i) { printf("%d ",ar[i]); } printf("\n"); free(ar); }
The result is:
2.calloc function:
- The function prototype is void* calloc(size_t num,size_t size);
- The function is to open up a space for num size elements, and initialize each byte of the space to 0.
- The only difference between calloc and malloc is that calloc initializes each byte of the requested space to all zeros before returning the address.
For example:
#include<stdio.h> #include<stdlib.h> void main() { int n; printf("input n:>"); scanf("%d", &n); //Portability //int *ar = (int*)malloc(sizeof(int) * n); int *ar = (int *)calloc(n, sizeof(int)); if(ar == NULL) { printf("Out Of Memory.\n"); return; } printf("Output results before assignment:\n"); for (int i = 0; i < n; ++i) { printf("%d ", ar[i]); } printf("\n"); for(int i=0; i<n; ++i) { ar[i] = i+1; } printf("Output result after assignment:\n"); for(int i=0; i<n; ++i) { printf("%d ",ar[i]); } printf("\n"); free(ar); }
The result is:
2.realloc function:
- The prototype of realloc function is void* realloc(void* ptr,size_t size)
- ptr is the memory address to be adjusted.
- Size is the new size after adjustment.
- The return value is the adjusted memory starting position.
- The realloc function will not only adjust the size of the original space, but also move the data in the original memory to the new space.
- There are two situations when realloc adjusts the memory space:
- There is enough space after the original space: at this time, the space is directly added after the original memory, and the original spatial data does not change.
2. There is not enough space after the original space: the expansion method is to find another continuous space of appropriate size on the heap space, and the function returns a new memory address.
For example:
#include<stdio.h> #include<stdlib.h> void main() { int n = 10; int *ar = (int*)malloc(sizeof(int) * n); if(ar == NULL) { printf("Out Of Memory.\n"); return; } for(int i=0; i<n; ++i) { ar[i] = i+1; } for(int i=0; i<n; ++i) { printf("%d ",ar[i]); } printf("\n"); int new_n = 20; ar = (int*)realloc(ar, sizeof(int)*new_n); for(int i=0; i<new_n; ++i) { printf("%d ",ar[i]); } printf("\n"); free(ar); }
The result is: