当前位置:网站首页>Dynamic memory management (malloc/calloc/realloc)
Dynamic memory management (malloc/calloc/realloc)
2022-07-05 23:05:00 【xaiobit_ hl】
1. Why is there dynamic memory allocation
Before learning dynamic memory allocation, the development methods we master are :

But there are two ways to open up space :
1. The size of the space opening is fixed .
2. Array is declared , Specify the length of the array , The memory he needs is allocated at compile time .
But the need for space , It's not just that . Sometimes the size of the space we need can only be known when the program is running , The way arrays open up space at compile time is not enough .
At this time, you can only try dynamic memory development .
2.malloc/calloc/realloc How is it used
malloc

calloc

realloc

3. Several classic written questions
3.1 Topic 1 :

3.2 Topic two :

3.3 Topic three

3.4 Topic four 
4. Flexible array
What is a flexible array ,C99 in , The last element in the structure is allowed to be an array of unknown size , This is called 【 Flexible array 】.
4.1 The characteristics of flexible arrays
- A flexible array member in a structure must be preceded by at least one other member .
- sizeof The size of the structure returned does not include the memory of the flexible array .
- Structures that contain flexible array members use malloc() Function to dynamically allocate memory , And the allocated memory should be larger than the size of the structure , To accommodate the period size of the flexible array .

4.2 The use of flexible arrays
So flexible array members a, It's equivalent to getting 100 A continuous space of two shaping elements .
4.3 The advantages of flexible arrays
Above type_a The structure can also be designed in this way

Above Code 1 and Code 2 It can do the same thing , however Code 1 There are two benefits to the implementation of :
The first benefit : Convenient memory release
If our code is in a function for others , You did it in there Secondary memory allocation , And return the whole structure to the user . The user calls free You can release the structure , But the user doesn't know that the members in the structure also need free, So you can't expect users to find out . therefore , If we put the memory of the structure and the memory of its members One off distribution Okay , And return a structure pointer to the user , user Do it once free You can also free up all the memory .
The second advantage : This will help to improve the access speed
Continuous memory is good for improving access speed , It also helps reduce memory fragmentation .
No ~
边栏推荐
- CorelDRAW plug-in -- GMS plug-in development -- new project -- macro recording -- VBA editing -- debugging skills -- CDR plug-in (2)
- Use of grpc interceptor
- Vcomp110.dll download -vcomp110 What if DLL is lost
- fibonacci search
- Judge whether the binary tree is a complete binary tree
- Expectation, variance and covariance
- 一文搞定JVM常见工具和优化策略
- 2022 registration examination for safety management personnel of hazardous chemical business units and simulated reexamination examination for safety management personnel of hazardous chemical busines
- Use the rewrite rule to rewrite all accesses to the a domain name to the B domain name
- Simple and beautiful method of PPT color matching
猜你喜欢

VOT toolkit environment configuration and use
![[screen recording] how to record in the OBS area](/img/34/bd06bd74edcdabaf678c8d7385cae9.jpg)
[screen recording] how to record in the OBS area

One article deals with the microstructure and instructions of class

一文搞定class的微觀結構和指令

Paddle Serving v0.9.0 重磅发布多机多卡分布式推理框架

Hcip day 12 (BGP black hole, anti ring, configuration)

February 13, 2022-4-symmetric binary tree

Event trigger requirements of the function called by the event trigger

The method and principle of viewing the last modification time of the web page

一文搞定JVM的内存结构
随机推荐
使用rewrite规则实现将所有到a域名的访问rewrite到b域名
30 optimization skills about mysql, super practical
Unity Max and min constraint adjustment
How to quickly understand complex businesses and systematically think about problems?
Global and Chinese market of water treatment technology 2022-2028: Research Report on technology, participants, trends, market size and share
Global and Chinese market of diesel fire pump 2022-2028: Research Report on technology, participants, trends, market size and share
Nacos installation and service registration
[untitled]
Element operation and element waiting in Web Automation
Ultrasonic sensor flash | LEGO eV3 Teaching
Simple and beautiful method of PPT color matching
Arduino measures AC current
One article deals with the microstructure and instructions of class
openresty ngx_lua请求响应
终于搞懂什么是动态规划的
VOT toolkit environment configuration and use
Activate function and its gradient
Leetcode buys and sells stocks
第十七周作业
Three. JS VR house viewing
