Article directory
1. Dynamic memory management
Dynamic memory management is provided by
- Memory application
- memory release
Composition, the memory here refers to heap memory, as opposed to stack memory;
During program running, dynamic memory management is often performed as needed to more flexibly manage memory resources, including:
- Allocate memory space in heap memory
- Release memory space in heap memory
In C language and C++ language, there are methods to dynamically allocate/release heap memory;
- In C language, it mainly involves the allocation and release of heap memory;
- In the C++ language, the main thing is the dynamic creation and release of objects;
2. Dynamic memory management in C language
1. C language memory application
In C language, use standard library functions such as malloc(), calloc(), realloc() and so on to dynamically apply for memory:
- malloc(size_t size): Allocate heap memory of the specified byte size, return a pointer to the heap memory space, or return NULL on failure;
- calloc(size_t num, size_t size): Allocate heap memory with the specified number of blocks and byte size. Compared with malloc, calloc automatically initializes the memory to 0;
- realloc(void* ptr, size_t size): Modify the byte size of the allocated memory block; if the pointer parameter ptr is NULL, this function has the same function as the malloc function; if the parameter size is 0, this function has the same function as the free function , used to release ptr memory;
Before calling the above function, you need to import the stdlib.h header file;
#include <stdlib.h>
2. C language memory release
In C language, call the free() standard library function to release the allocated memory;
3. Code example - C language dynamic memory management
In the code below,
First, use the malloc() function to dynamically apply for heap memory that can store 5 int data.
// 函数原型 :
void *malloc(unsigned int size);
Then, convert the void * type pointer returned by the malloc function into an int * type pointer;
// 函数原型 :
// void *malloc(unsigned int size);
// 申请内存
int* array = (int*)malloc(5 * sizeof(int));
Then, use array subscripts to access the memory and assign values to the memory;
// 为内存赋值
// 赋值时使用数组下标的方式访问内存
for (int i = 0; i < 5; i++) {
array[i] = i;
}
Then, print the memory data and use pointers to access the memory when obtaining the memory space value;
// 打印内存
// 获取内存空间值时使用指针形式访问内存
for (int i = 0; i < 5; i++) {
printf("%d ", *(array + i));
}
Finally, call the free function to release this memory;
// 释放内存
free(array);
Code example:
#include <stdio.h>
#include <stdlib.h>
int main() {
// 申请内存
int* array = (int*)malloc(5 * sizeof(int));
if (array == NULL) {
printf("内存分配失败\n");
return -1;
}
// 为内存赋值
// 赋值时使用数组下标的方式访问内存
for (int i = 0; i < 5; i++) {
array[i] = i;
}
// 打印内存
// 获取内存空间值时使用指针形式访问内存
for (int i = 0; i < 5; i++) {
printf("%d ", *(array + i));
}
printf("\n");
// 释放内存
free(array);
// 控制台暂停 , 按任意键继续向后执行
system("pause");
return 0;
}
Results of the :
0 1 2 3 4
请按任意键继续. . .