当前位置:网站首页>Dynamic memory management
Dynamic memory management
2022-04-23 02:01:00 【Populus euphratica under the desert】
Personal home page : Welcome to ——> Populus euphratica under the desert
Ladies and gentlemen , It's beautiful
If you think the article is helpful to you
You can support bloggers with one key and three links
Your every care is the driving force of my persistence
![]()
: This issue focuses on : Knowledge of dynamic memory
I hope everyone can study and work happily every day
Why dynamic memory development ?
What is dynamic memory ?
Dynamic memory is to use malloc,calloc,realloc and free These dynamic memory functions are used to request space from the heap , To the programmer for management and use , This is dynamic memory management .
malloc Use
malloc Is to apply for a space , You need to initialize manually , And manually free And empty
calloc
calloc Is to open up space num individual sizeof( type ) And initialize to 0.
realloc
realloc There are two situations :
1. When the source space is 0, The incoming NULL when , and malloc It works the same .
2. If you expand the space , That is, when the passed in pointer is valid , There are two cases
2.1. If there is enough space behind the original space , Then open up directly behind the original space , Returns the lowest address of the original space .( That is, the return value is the same as the incoming value )
2.2. If there is not enough space behind the original space , Then the function will be on the heap , Find a space that can meet the space size required by users , Then copy the original spatial data to the space opened by the function , Then release the original space , Return the lowest address of the reopened space .
This is generally rare , So no more examples .
C Program address space
First of all, we need to understand that the stacking area is C Where is the program address space ? Or say C What is the program address space ?
Today we mainly talk about the pile area .
Why dynamic memory development ?
First of all, we know that the normal application space can be applied directly on the stack area , So why do we need dynamic memory development ?
for instance :
We need to store some numbers , These numbers can be many , There may be very little , At this time, how can we apply for space in the stack area ?
In the idea of saving everything , We need to open up more space , But the stack space is limited , A little bigger will cause stack overflow , That's why there's a heap area .
Error accessing memory
For example, define a structure , Among them, the structure members are dynamically opened up , If not initialized, use , Then it will lead to an error in accessing memory .
struct stu { char* p; int age; }s1; int main() { strcpy(s1.p, " Under Populus euphratica "); s1.age = 18; return 0; }there s1.p Although it's a variable , But there is no space for relativity , So it will lead to an error in accessing memory .
It says , It should be given to s.p Open up space , Let's see how to open it up , The following development is no problem .
struct stu { char* p; int age; }s1; void Show(char *name) { printf("%s\n", name); } int main() { struct stu s1 = { NULL, 0 }; s1.p = (char *)malloc(sizeof(char)* 30); strcpy(s1.p, " Under Populus euphratica "); Show(s1.p); free(s1.p); s1.p = NULL; return 0; }
Pointer legitimacy problem
First , what " legal " The pointer of ?
Generally speaking , Is that the passed pointer can be used normally .
If a pointer has a point ( The wild pointer also has a point , Just don't know where to point ), Then we can't judge " legal " sex . So we ask that if the pointer is not used directly , Then it should be assigned to NULL, So we can verify ” legal “ When it comes to sex , It becomes to judge whether the pointer is NULL.
After improving the above code, it will become the following code :
struct stu { char* p; int age; }s1; void Show(char *name) { if (name == NULL) { exit(-1); } printf("%s\n", name); } int main() { struct stu s1 = { NULL, 0 }; s1.p = (char *)malloc(sizeof(char)* 30); if (s1.p == NULL) { exit(-1); } strcpy(s1.p, " Under Populus euphratica "); Show(s1.p); free(s1.p); s1.p = NULL; return 0; }Here we judge whether the pointer is NULL, This avoids null pointers . You can also use assert( macro ) To assert that , Reference header file "assert.h", However, this macro can only be used in the debug version , Cannot be used for release version .
Memory out of bounds
I believe everyone has heard of the topic of memory cross-border , Will the pointer cross the boundary and report an error ?
Sometimes when the pointer is out of bounds, an error will not be reported , such as :
perhaps malloc There was no free There is also a probability of not reporting an error .
But no free More serious things will happen , It's called a memory leak .
Memory leak
Memory leaks are simply , The program only requests memory , No release, No free.
So if there is no program , Or the program exits , So is there a memory leak problem ?
If the program exits , Then the operating system will forcibly reclaim the requested memory .
The biggest fear of memory leakage is some programs that will never quit voluntarily , Then it's a terrible thing , A process , Only apply for memory , Don't release , Then there is no memory available .
free How much space ?
We only know free From , So how big is the release ?
Let's take a look at ,free The size of the is much larger than the free space , So you can say , I malloc It's bigger than we're applying for .
because malloc To apply for space, there should be extra space to manage the applied space , So it's bigger than what we normally apply for , So if we malloc If you want to cross the border, you don't have to report an error .
free What is the release ?
free After that, we usually set the pointer to null , Why? ? Will it still point to the original space if it is not empty ?
We can see that the direction of space has not changed , In other words, the direction has not changed , So what has changed ?
The change is p Relationship with the heap application , therefore free Is to change the relationship ,free The essence is to change the relationship , in other words p It also points to the space applied for in the heap area , So we should break this relationship , Just put p Set as NULL.
It's like talking about your girlfriend , It's all broken up , You shouldn't leave your contact information , So it's time to break the contact information .
Next up :
In the next issue, we'll talk about functions
The next issue is more exciting ~!~!~!

版权声明
本文为[Populus euphratica under the desert]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220845286306.html
边栏推荐
- What is an API interface?
- How to write the resume of Software Test Engineer so that HR can see it?
- What are the test steps of dynamic proxy IP?
- 客户端项目管理经常面临的挑战
- 2018 China Collegiate Programming Contest - Guilin Site J. stone game
- 搭建个人主页保姆级教程(二)
- 批处理多个文件合成一个HEX
- 使用代理IP是需要注意什么?
- Batch multiple files into one hex
- ESP32使用freeRTOS的消息队列
猜你喜欢

Batch multiple files into one hex

How to write the resume of Software Test Engineer so that HR can see it?

W801 / w800 WiFi socket development (I) - UDP

Easyswool environment configuration

W801 / w800 WiFi socket development (II) - UDP Bluetooth control WiFi connection

教程】如何用GCC“零汇编”白嫖MDK

Longest common subsequence (record path version)

Redis memory recycling strategy

About how to import C4d animation into lumion

Performance introduction of the first new version of cdr2022
随机推荐
Echo "new password" |passwd -- stdin user name
动态代理ip的测试步骤有哪些?
Shardingsphere broadcast table and binding table
Realize linear regression with tensorflow (including problems and solutions in the process)
拨号vps会遇到什么问题?
What code needs unit testing?
关于C4D动画如何导入Lumion
Leetcode 112 Total path (2022.04.22)
中金财富跟中金公司是一家公司吗,安全吗
Analyze the advantages and disadvantages of tunnel proxy IP.
JSP basic knowledge summary
How to classify proxy IP?
Implementation of Base64 encoding / decoding in C language
2022第六季完美童模 IPA國民賽領跑元宇宙賽道
2022 crane driver (limited to bridge crane) examination question bank and online simulation examination
什么是bgp服务器,有哪些优势?
Dimension C China helping farmers in rural areas warms people's hearts the third stop is jiabaoguo farm
NPM yarn startup error [resolved]
2022.4.20-----leetcode. three hundred and eighty-eight
PHP & laravel & master several ways of generating token by API and some precautions (PIT)








Today we mainly talk about the pile area .



