当前位置:网站首页>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 .
 Insert picture description here
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.

 Insert picture description here

Make sure you use C Language to implement this simple dynamic memory allocation .
 Insert picture description here
The final terminal output results are as follows :
 Insert picture description here
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
 Insert picture description here
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 .
 Insert picture description here
The picture above shows a bit Mapping to 64Byte, such :
 Insert picture description here
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

原网站

版权声明
本文为[HQYJ_]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202181333428833.html