当前位置:网站首页>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
边栏推荐
- CC2541的仿真器CC Debugger使用教程
- Shardingsphere sub database and sub table
- Today will finally write system out. Println()
- How to classify proxy IP?
- Longest common subsequence (record path version)
- Leetcode 112 Total path (2022.04.22)
- Encrypted compressed backup bat script
- 2022第六季完美童模 IPA國民賽領跑元宇宙賽道
- Under the pressure of sales, domestic mobile phones began to reduce prices, but they haven't put down their final face
- [tutorial] how to use GCC "zero assembly" for white whoring MDK
猜你喜欢

Shardingsphere read write separation

Is it better to use a physical machine or a virtual machine to build a website?

What is a dial-up server and what is its use?

一加一为什么等于二

2022 Saison 6 perfect Kid Model IPA national race Leading the Meta - Universe Track

leetcode:27. 移除元素【count remove小操作】

Analyze the three functions of static proxy IP.

What problems will you encounter when dialing VPS?

浅析静态代理ip的三大作用。

客户端项目管理经常面临的挑战
随机推荐
J-link v9 使用技巧之虚拟串口功能
Is the availability of proxy IP equal to the efficiency of proxy IP?
2022.4.10-----leetcode. eight hundred and four
MySQL active / standby configuration binary log problem
Basic knowledge of software testing, you can meet the interviewer after reading it
What is an API interface?
What is BGP server and what are its advantages?
如何设置电脑ip?
Performance introduction of the first new version of cdr2022
EBS:PO_ EMPLOYEE_ HIERARCHIES_ ALL
2022.4.20-----leetcode.388
Quel est le fichier makefile?
Unity editor hierarchy drop-down menu extension
Uncover floating-point operations hidden by the ARM compiler
2022.4.20-----leetcode. three hundred and eighty-eight
Why is one plus one equal to two
Encrypted compressed backup bat script
What are the test steps of dynamic proxy IP?
Challenges often faced by client project management
世界读书日 | 技术人不要错过的好书(IT前沿技术)








Today we mainly talk about the pile area .



