C commonly used dynamic memory development functions are malloc,calloc,realloc. Let's explain the usage of these three functions first.
1.malloc
Header file: #include < stdlib h>
Reference format: void* malloc(size_t size)
Meaning: apply for a space of size bytes from memory and return the starting address of the block space. If the development fails, NULL will be returned
Note that the default return type is void *. In actual use, the type you want to forcibly convert to will be added in front of malloc
Usage example:
int* p = (int*)malloc(10 * sizeof(int));//Can be written as (int*)malloc(40); //After malloc is applied, you must check whether the application is successful //If the request fails, a NULL is returned if (p == NULL) { printf("%s\n", strerror(errno));//Print out the error information corresponding to the corresponding error code } else { //... }
2.calloc
Header file: #include < stdlib h>
Reference format: void* calloc(size_t num,size_t size)
num number of elements
size - number of bytes per element
Meaning: apply for numsize byte space from memory, return the starting address * of the block space, and initialize the element values to 0. If the development fails, NULL will be returned.
Usage example:
int*p=(int*)calloc(10, sizeof(int)); if (p = NULL) { printf("%s\n", strerror(errno)); } else { //... }
3.realloc
Header file: #include < stdlib h>
Reference format: void* realloc(void*memblock,size_t size)
memblock - address of the space to be adjusted
Size - adjusted memory size (not new size)
Meaning: realize capacity expansion and return the starting address of the new space. NULL will be returned when the expansion fails.
This function can be used in two cases:
1. If there is enough space behind the original application space to add, add it directly behind the original space and return to the original address
2. If there is not enough space to add, the realloc function will find a new memory area, open up a new space to meet the needs, then copy the contents of the original application space to the new space, release the original space, and return the address of the new space.
Usage example:
int main() { int *p=(int*)malloc(20 * sizeof(int)); if (p == NULL) { printf("%s\n", strerror(errno)); } else { //... } //If you find that 20 bytes are not enough and want to open up 40 bytes of space, you can use the realloc function to adjust the size of the space int* ptr=(int*)realloc(p, 40 * sizeof(int)); //be careful!!! After expanding the space, it cannot be directly assigned to the original address. //Because if the application fails, the returned value is null. At this time, if it is directly assigned to the original address, the data on the original address will be lost if (ptr == NULL) { printf("%s\n", strerror(errno)); } else { //... }
If the first parameter of realloc is set to NULL, it can also be directly used to open up space. At this time, it is equivalent to malloc:
int* p = realloc(NULL, 40);//Equivalent to malloc(40)
The function of dynamic memory release commonly used in C is free. For malloc,realloc,calloc and other spaces opened up through dynamic memory, they must be released through free
int main() { int* p = (int*)malloc(10 * sizeof(int)); //When the dynamically applied space is no longer used, the space needs to be returned free(p); return 0; }
The function used for dynamic memory development in C + + is new Note that new is an operator, but malloc\calloc\realloc are all functions.
(1) For built-in types (char,short,int,float,double, pointer, etc.), new is the same as malloc, but the writing method is different
int main() { //Apply for a group of spaces //C int* p1 = (int*)malloc(sizeof(int) * 10); //C++ int* p2 = new int[10]; //Apply for a single object //C int* p3 = (int*)malloc(sizeof(int)); //C++ int* p4 = new int; //C free(p1); free(p3); //C++ delete [] p2; delete p4; return 0; }
In addition, when you use new to create space for built-in types, you can also bring your own initialization, for example:
int* p1 = new int(1);//Parentheses are used to assign the initial value of a single data, and square brackets are used to declare the space size when applying for multiple spaces int* p2 = new int[4]{ 1,2,3,4 };//Don't write an equal sign
(2) For custom types, such as classes, malloc only has open space, and new includes open space plus constructor initialization`
class ListNode { private: int _val; ListNode* prev; ListNode* next; public: ListNode() { prev = nullptr; next = nullptr; _val = 0; } }; int main() { ListNode* p1 = (ListNode*)malloc(sizeof(ListNode)); ListNode* p2 = new ListNode; return 0; }
You can see that the three member variables in p1 are random values, p
All in 2 have been initialized according to the constructor.
The commonly used dynamic memory release function in C + + is delete. Similarly, the difference between delete and free is:
For built-in types, both are the same;
For custom types,
free only frees up space, and delete includes destructor cleanup + frees up space for user-defined types