[Dynamic memory management]

Tip: After the article is written, the table of contents can be automatically generated. For how to generate it, please refer to the help document on the right.

Table of contents

Preface

1. Why is dynamic memory allocation necessary?

2. malloc and free

2.1 malloc

2.2 free

3. calloc and realloc

3.1 calloc

3.2 realloc

4. Common dynamic memory errors

4.1 Dereference operation of NULL pointer

4.2 Out-of-bounds access to dynamically opened space

4.3 Use free release for non-dynamically allocated memory

4.4 Use free to release a part of dynamically allocated memory

4.5 Release the same dynamic memory multiple times

4.6 Dynamically allocated memory and forgets to release it (memory leak)

5. Analysis of classic written test questions on dynamic memory

5.1 Question 1:

5.2 Question 2:

5.3 Question 3:

5.4 Question 4:

5.5 Question 5:

6. Flexible array

6.1 Characteristics of flexible arrays:

6.2 Use of flexible arrays

6.3 Advantages of flexible arrays

7. Summarize the division of program memory areas in C/C++

Summarize


Preface

世上有两种耀眼的光芒,一种是正在升起的太阳,一种是正在努力学习编程的你!一个爱学编程的人。各位看官,我衷心的希望这篇博客能对你们有所帮助,同时也希望各位看官能对我的文章给与点评,希望我们能够携手共同促进进步,在编程的道路上越走越远!

像回顾上一节博客的,请点击这里自定义类型:联合和枚举


提示:以下是本篇文章正文内容,下面案例可供参考

1. Why is dynamic memory allocation necessary?

The way to open up space has two characteristics:

• The space allocation size is fixed.

• When declaring an array, the length of the array must be specified. Once the size of the array space is determined, it cannot be adjusted. However, the demand for space is not limited to the above situations. Sometimes the amount of space we need can only be known when the program is running, so the method of creating space during compilation of the array is not sufficient.

C language introduces dynamic memory allocation, which allows programmers to apply for and release space by themselves, which is more flexible.

2. malloc and free

2.1 malloc

C language provides a dynamic memory allocation function:

void* malloc (size_t size);

This function applies for a continuous available space in memory and returns a pointer to this space.

• If the allocation is successful, a pointer to the allocated space is returned.

• If the allocation fails, a NULL pointer will be 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 space to be opened. The user must decide by himself when using it.

• If the size parameter is 0, the behavior of malloc is undefined by the standard and depends on the compiler.

How to recycle the space requested by malloc?

1: free recycling

2: If you do not release it yourself, it will be recycled by the operating system after the program ends.

3: malloc applies for memory in the heap area 

2.2 free

C language provides another function free, which is specially used to release and recycle dynamic memory. The function prototype is as follows: < /span>

 void free (void* ptr);

The free function is used to release dynamically allocated memory.

• If the space pointed to by parameter ptr is not dynamically allocated, the behavior of the free function is undefined.

• If the parameter ptr is a NULL pointer, the function does nothing.

Both malloc and free are declared in the stdlib.h header file.

3. calloc and realloc

3.1 calloc

C language also provides a function called calloc. The calloc function is also used for dynamic memory allocation. The prototype is as follows:

void* calloc (size_t num, size_t size);
//第一个参数:申请的数组有多少个元素个数
//第二个参数:数组中每一个元素的字节大小

The differences between the calloc() function and the malloc() function:
1: The parameters of the calloc() function and the malloc() function are different
2: The callloc() function will apply for space on the heap area, initialize the value in the space to 0 by default, and return the starting address of the space, while the malloc() function will only apply for space on the heap area and return the space's Starting address

for example:

So if we require initialization of the applied memory space, then it can be very convenient Use the calloc function to complete the task.

3.2 realloc

• The emergence of realloc function makes dynamic memory management more flexible.

• Sometimes we find that the space we applied for in the past is too small, and sometimes we feel that the space we applied for is too large. In order to use the memory reasonably, we must make flexible adjustments to the memory size. Then the realloc function can adjust the dynamically allocated memory size.

The function prototype is as follows:

void* realloc (void* ptr, size_t size);

• ptr is the memory address to be adjusted

• size new size after adjustment

• The return value is the starting position of the memory after adjustment.

• This function not only adjusts the size of the original memory space, but also moves the data in the original memory to the new space.

• There are two situations when realloc adjusts the memory space:

◦ Case 1: There is a large enough space after the original space

◦ Case 2: There is not enough space after the original space

Case 1

When it is case 1, if you want to expand the memory, just add space directly after the original memory, and the data in the original space will not change.

Case 2

When it is case 2 and there is not enough space after the original space, the expansion method is: find another continuous space of suitable size on the heap space Use. In this way, the function returns anew memory address.

 The first parameter of the realloc() function is the memory address to be adjusted. If the first parameter is NULL, the function of the realloc() function is equivalent to the malloc() function.

4. Common dynamic memory errors

4.1 Dereference operation of NULL pointer

void test()
 {
 int *p = (int *)malloc(INT_MAX/4);
 *p = 20;//如果p的值是NULL,就会有问题
//(所以要判断一下p是否为空指针)
 free(p);
 }

4.2 Out-of-bounds access to dynamically opened space

void test()
 {
 int i = 0;
 int *p = (int *)malloc(10*sizeof(int));
 if(NULL == p)
 {
 exit(EXIT_FAILURE);
 }
 for(i=0; i<=10; i++)
 {
 *(p+i) = i;//当i是10的时候越界访问
 }
 free(p);
 }

4.3 Use free release for non-dynamically allocated memory

void test()
 {
 int a = 10;
 int *p = &a;
 free(p);//ok?
 }

4.4 Use free to release a part of dynamically allocated memory

void test()
 {
 int *p = (int *)malloc(100);
 p++;
 free(p);//p不再指向动态内存的起始位置
 }

4.5 Release the same dynamic memory multiple times

void test()
 {
 int *p = (int *)malloc(100);
 free(p);
 free(p);//重复释放
 }

4.6 Dynamically allocated memory and forgets to release it (memory leak)

Forgetting to release dynamically allocated space that is no longer used can cause memory leaks.

Remember:The dynamically allocated space must be released and released correctly.

5. Analysis of classic written test questions on dynamic memory

5.1 Question 1:

Modification one:

Modification 2:

5.2 Question 2:

5.3 Question 3:

5.4 Question 4:

5.5 Question 5:

6. Flexible array

Maybe you have never heard of the concept of flexible array, but it does exist.

In C99, the last element in a structure is allowed to be an array of unknown size. This is called a "flexible array" member.

1: In structure
2: Last member
3: Array of unknown size (flexible array)
Use with dynamic memory management

Code demo:

typedef struct st_type
{
 int i;

 int a[0];//柔性数组成员
}type_a;

Some compilers will report an error and cannot compile. You can change it to:

typedef struct st_type
{
 int i;
 int a[];//柔性数组成员
}type_a;

6.1 Characteristics of flexible arrays:

• Flexible array members in a structure must be preceded by at least one other member.

• The size of the structure returned by sizeof does not include the memory of the flex array.

• The structure containing flexible array members uses the malloc () function to dynamically allocate memory, and the allocated memory should be larger than the size of the structure to accommodate the expected size of the flexible array.

Code demo:

typedef struct st_type
{
 int i;
 int a[0];//柔性数组成员
}type_a;
int main()
{
 printf("%d\n", sizeof(type_a));//输出的是4
 return 0;
}

6.2 Use of flexible arrays

 Code one:

6.3 Advantages of flexible arrays

Code two: 

The above code 1 and code 2 can accomplish the same function, but the implementation of method 1 has two benefits:

The first benefit is:Convenient memory release

If our code is in a function for others to use, you make a secondary memory allocation in it and return the entire structure to the user. The user can release the structure by calling free, but the user does not know that the members in the structure also need to be free, so you cannot expect the user to discover this. Therefore, if we allocate the memory of the structure and the memory required by its members at once, and return a structure pointer to the user, the user can free all the memory once.

The second benefit is:This is beneficial to access speed

Contiguous memory is beneficial to improving access speed and reducing memory fragmentation. (Actually, I personally don’t think it’s much higher. Anyway, you can’t run and you have to use offset addition to address)

7. Summarize the division of program memory areas in C/C++

Several areas of memory allocation for C/C++ programs:

1. Stack area (stack): When executing a function, the storage units of local variables within the function can be created on the stack, and these storage units are automatically released when the function ends. The stack memory allocation operation is built into the processor's instruction set and is very efficient, but the allocated memory capacity is limited. The stack area mainly stores local variables, function parameters, return data, return addresses, etc. allocated for running functions.

2. Heap area (heap): Generally allocated and released by the programmer. If the programmer does not release it, it may be recycled by the operating system when the program ends. The allocation method is similar to a linked list.

3. The data segment (static area) stores global variables and static data. It is released by the system after the program ends.

4. Code segment: stores the binary code of the function body (class member function and global function).​ 


Summarize

Okay, this blog ends here. If you have a better point of view, please leave a message in time. I will watch it carefully and learn from it.
If you don’t accumulate steps, you can’t reach a thousand miles; if you don’t accumulate small streams, you can’t become a river or sea.

Guess you like

Origin blog.csdn.net/2301_79585944/article/details/134361056