当前位置:网站首页>C language dynamic memory allocation
C language dynamic memory allocation
2022-06-25 21:11:00 【HQYJ_】
First, understand why dynamic memory allocation is required , be familiar with C Language readers should be familiar with this , It is used when a piece of memory is needed malloc Function to request the required amount of memory , Function returns the first address of a section of memory . Simply speaking , The advantage of dynamic memory allocation is that when memory is needed, it can be allocated on demand , Memory can be released when it is not needed , This makes efficient use of memory . The following article implements a complete dynamic memory allocation from scratch .
Simple dynamic memory allocation implementation
Memory allocation is to allocate unused memory blocks to required variables ( Common variables 、 Pointer to the variable 、 Structural variables, etc ) Use , Because it needs to be released after use , This will cause the free memory to be scattered in the memory pool . therefore , Memory must be managed , That is, mark the memory usage .
The above figure shows a certain time after a memory pool is used , You can see , Used blocks and unused blocks are not contiguous , So you need to tag it with a table , This watch is called BitMap. Suppose that the memory is now divided into each Byte division , Then use one bit Mark blocks ,1 Indicates that... Has been used ,0 Indicates not used , Such a block requires a bit.
Make sure you use C Language to implement this simple dynamic memory allocation .
The final terminal output results are as follows :
The above has implemented a simple dynamic memory allocation , Can complete the allocation and release of memory, output utilization and view bitmap . The dynamic memory allocation implemented in this way will not generate internal fragments , That's the advantage of it , But the obvious disadvantage is that the utilization rate is too low .
Practical dynamic memory allocation
Careful readers may have noticed that the simple dynamic memory allocation above has a drawback , It's just one. bit Only one byte can be represented , That is to say, it means 8 A byte requires a byte bitmap , This mapping causes its memory to
This is a waste in many cases . In order to improve utilization , You must increase the granularity of the mapping block , That's one Bit The mapping range of corresponds to multiple bytes .
The picture above shows a bit Mapping to 64Byte, such :
Although the utilization rate has become higher , But it can produce internal debris , The so-called internal fragmentation is the memory space that cannot be used within the minimum granularity , Why is this space unusable , The reason is that when applying for memory blocks , Its memory can only be 64B The alignment of the , Even if less than 64B, You have to press 64B Come and see , Because this granularity has been bitmap The tag uses , When next used , It cannot be assigned . therefore , You can see , The larger the particle size , The larger the internal memory fragments it may generate , Memory utilization and fragmentation need to be balanced , A good algorithm can only solve the problem of external fragmentation , Unable to resolve internal fragmentation problem , Therefore, the dynamic memory allocation must be balanced , To achieve the best results .
Free learning materials : Learning exchange group :943552345
1 Hours to get you started Linux Next C Language development
http://www.makeru.com.cn/live/5413_2337.html?s=10
c Code debugging is more efficient in this way
http://www.makeru.com.cn/live/5413_2302.html?s=10
COVID-19 management system , How to use code to realize ?
http://www.makeru.com.cn/live/5413_2269.html?s=10
Understand documents in a class IO With the standard IO
http://www.makeru.com.cn/live/5413_2293.html?s=10
Give you some tips to improve the efficiency of self-study
http://www.makeru.com.cn/live/5413_2246.html?s=10
About stacks , What will the interviewer test
http://www.makeru.com.cn/live/5413_2230.html?s=10
c The storage type of variables in a language
http://www.makeru.com.cn/live/5413_2153.html?s=10
Necessary for entry-level Xiaomeng NEW : Relationship between macro definition and function
http://www.makeru.com.cn/live/5413_2145.html?s=10
C Fundamentals of language programming
http://www.makeru.com.cn/live/1758_311.html?s=10
C Language ( series “ Click under the title to start learning ”)
http://www.makeru.com.cn/course/details/2233?s=10
promote C Programming ability
http://www.makeru.com.cn/live/1392_1166.html?s=10
Tamp C Language , The advanced road from Xiaobai to Daniel !
http://www.makeru.com.cn/live/5413_1980.html?s=10
The pointer
http://www.makeru.com.cn/live/1392_238.html?s=10
Do you still know the pointer
http://www.makeru.com.cn/live/5413_2043.html?s=10
C Language control led The lamp
http://www.makeru.com.cn/live/1392_304.html?s=10
Input and output
http://www.makeru.com.cn/live/1758_312.html?s=10
C Language to achieve object-oriented programming
http://www.makeru.com.cn/live/1392_1051.html?s=10
Popularization and application of structure
http://www.makeru.com.cn/live/5413_1909.html?s=10
C Language play linked list
http://www.makeru.com.cn/live/1392_338.html?s=10
data type 、 Constant 、 Variables and operators
http://www.makeru.com.cn/video/1877.html?s=10
C Classic practical cases of language and data structure
http://www.makeru.com.cn/live/5413_2014.html?s=10
Linux C Advanced language development
http://www.makeru.com.cn/course/details/2478?s=10
necessary Linux Command and C Language foundation
http://www.makeru.com.cn/video/1862.html?s=10
边栏推荐
- Local variables and global variables in C language
- Detailed explanation of unified monitoring function of multi cloud virtual machine
- What is machine learning? (Fundamentals)
- Yunzhisheng atlas supercomputing platform: computing acceleration practice based on fluid + alluxio (Part 2)
- Leetcode theme [array] -31- next spread
- Nine built-in objects of JSP and four scopes of Servlet
- 同花顺炒股软件开户是合法的吗?安全吗
- couldn‘t be accessed by user ‘_ apt‘
- Day 28/100 CI CD basic introductory concepts
- Online yaml to XML tool
猜你喜欢
What is a server? (Powercert animated videos)
R language quantile autoregressive QAR analysis pain index: time series of unemployment rate and inflation rate
Feature Engineering in simple terms – a practice guide based on openmldb (Part 1)
UDP Vs TCP (Powercert animated videos)
[data recovery in North Asia] a data recovery case in which the upper virtual machine data is lost due to the hard disk failure and disconnection of raid6 disk array
Exploration of advanced document editor design in online Era
Yunzhisheng atlas supercomputing platform: computing acceleration practice based on fluid + alluxio (Part I)
A simple file searcher
[opencv] opencv from introduction to mastery -- detailed explanation of input and output XML and yaml files
New generation engineers teach you how to play with alluxio + ml (Part 2)
随机推荐
Idea implements hot deployment
ThreadLocal class
Cloud native 04: use envoy + open policy agent as the pre agent
What is DNS (domain name server)? (Powercert animated videos)
05 configuring network parameters
Cross project measurement is a good helper for CTOs and PMOS
HMS core actively explores the function based on hardware ear return, helping to reduce the overall singing delay rate of the singing bar by 60%
Tencent music knowledge map search practice
Soft test intermediate simple notes
[buucry] sensor (Manchester code)
Get the root directory of the package at compile time from buildreoot
Lesson 3 urllib
启牛学堂证券开户安全嘛?
Rounding related calculation
Node connection MySQL
How to close gracefully after using jedis
Lesson 1 Preparation
Sqlmap for interface security testing
Server pressure troubleshooting top
What is a subnet mask? (Powercert animated videos)