当前位置:网站首页>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 ~
边栏推荐
- Global and Chinese markets for children's amusement facilities 2022-2028: Research Report on technology, participants, trends, market size and share
- Use of shell:for loop
- Leetcode weekly The 280 game of the week is still difficult for the special game of the week's beauty team ~ simple simulation + hash parity count + sorting simulation traversal
- 媒体查询:引入资源
- Go语言实现原理——Map实现原理
- How to quickly understand complex businesses and systematically think about problems?
- Expectation, variance and covariance
- 判断二叉树是否为完全二叉树
- 两数之和、三数之和(排序+双指针)
- openresty ngx_lua请求响应
猜你喜欢
[speech processing] speech signal denoising and denoising based on Matlab GUI low-pass filter [including Matlab source code 1708]
Go语言实现原理——Map实现原理
Thoroughly understand JVM class loading subsystem
实现反向代理客户端IP透传
[digital signal denoising] improved wavelet modulus maxima digital signal denoising based on MATLAB [including Matlab source code 1710]
Negative sampling
Hcip day 12 (BGP black hole, anti ring, configuration)
Activate function and its gradient
Finally understand what dynamic planning is
Simple and beautiful method of PPT color matching
随机推荐
第十七周作业
Distributed solution selection
2022.02.13 - SX10-30. Home raiding II
Paddy serving v0.9.0 heavy release multi machine multi card distributed reasoning framework
秒杀系统的设计与实现思路
Getting started stm32--gpio (running lantern) (nanny level)
并查集实践
二叉树递归套路总结
Three.js-01 入门
February 13, 2022-4-symmetric binary tree
The maximum happiness of the party
Krypton Factor-紫书第七章暴力求解
[screen recording] how to record in the OBS area
The difference between MVVM and MVC
两数之和、三数之和(排序+双指针)
Expectation, variance and covariance
Global and Chinese markets for reciprocating seal compressors 2022-2028: Research Report on technology, participants, trends, market size and share
First, redis summarizes the installation types
Leetcode weekly The 280 game of the week is still difficult for the special game of the week's beauty team ~ simple simulation + hash parity count + sorting simulation traversal
终于搞懂什么是动态规划的