当前位置:网站首页>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
边栏推荐
- How to initialize "naming and surname" in C language
- 如何设置电脑ip?
- Cc2541 emulator CC debugger tutorial
- How to set computer IP?
- Encrypted compressed backup bat script
- Longest common subsequence (record path version)
- 一些使用代理IP的小技巧。
- Nanny level tutorial on building personal home page (II)
- Basic knowledge of software testing, you can meet the interviewer after reading it
- 揭秘被Arm编译器所隐藏的浮点运算
猜你喜欢
【动手学深度学习V2】循环神经网络-1.序列模型
When should I write unit tests? What is TDD?
Nanny level tutorial on building personal home page (II)
App optimization and advanced scoreboard Part 2 [Mui + flask + mongodb]
Digital collection platform settled in digital collection platform to develop SaaS platform of digital collection
一些使用代理IP的小技巧。
Shardingsphere sub database and sub table
2022第六季完美童模 IPA国民赛领跑元宇宙赛道
有哪些常见的代理ip问题?
2022 Saison 6 perfect Kid Model IPA national race Leading the Meta - Universe Track
随机推荐
W801 / w800 / w806 unique ID / CPUID / flashid
W801 / w800 WiFi socket development (II) - UDP Bluetooth control WiFi connection
Echo "new password" |passwd -- stdin user name
J-link v9 使用技巧之虚拟串口功能
What categories do you need to know before using proxy IP?
What are the common proxy IP problems?
Shardingsphere broadcast table and binding table
2022 Saison 6 perfect Kid Model IPA national race Leading the Meta - Universe Track
What are the benefits of writing unit tests using the unit test framework?
力扣(LeetCode)112. 路径总和(2022.04.22)
JSP basic knowledge summary
《维C中国》乡村助农暖人心第三站嘉宝果农场
Is it better to use a physical machine or a virtual machine to build a website?
What is BGP server and what are its advantages?
Digital collection platform settled in digital collection platform to develop SaaS platform of digital collection
电子采购如何成为供应链中的增值功能?
有哪些常见的代理ip问题?
Thinkphp内核开发盲盒商城源码v2.0 对接易支付/阿里云短信/七牛云存储
搭建网站是用物理机还是云主机好?
Keil MDK Chinese garbled code, two solutions, the font is no longer ugly