当前位置:网站首页>[dynamic memory allocation]
[dynamic memory allocation]
2022-07-18 15:07:00 【Pigskin brother】
🧸🧸🧸 Hello, everyone , I'm pig skin brother 🧸🧸🧸

Today's content is Dynamic memory allocation .
Here is the following knowledge content 🥳🥳🥳
One 、 Preface : Why is there dynamic memory allocation
Why is there dynamic memory allocation ?
int val = 20;// Open up four bytes in the stack space
char arr[10]={0};// Open up ten bytes of continuous space on the stack space
But these two development methods have two characteristics
1. The size of the space opening is fixed
2. When an array is declared , You must specify the length of the array , The memory it needs is allocated at compile time .
Two 、 Common area division in memory
In memory , Common areas are
The stack area 、 Heap area 、 Static zone 、 Constant area, etc
Local variables are stored on the stack , The usage habit of stack area is to use high address first and then low address
Static variables are stored in the static area
And the space opened up by dynamic memory , It was opened up on the stacking area ,
for instance malloc,realloc,calloc
3、 ... and 、 Dynamic memory exploit function
1、malloc
malloc The use of requires a header file stdlib.h perhaps malloc.h
malloc The development of will find a space on the heap , If the development fails, a null pointer will be returned
You can see ,malloc The return type of is void*, therefore , When receiving the return value , need Cast
malloc Usage of :
int main()
{
//int arr1[10];//40 Bytes
//char arr2[40];//40 Bytes
void*malloc(size_t);
//int* arr3 = (int*)malloc(sizeof(int) * 10);// Apply in the stacking area
//char* arr4 = (char*)malloc(sizeof(char) * 40);
int* p = (int*)malloc(sizeof(int) * 10);
int* ptr = p;
if (p==NULL)
{
perror("malloc");
return 1;
}
int i = 0;
for (int i = 0; i < 10; i++)
{
*p = i;
p++;
printf("%d ",*p);
}
free(ptr);
ptr = NULL;
//free After that, this space no longer belongs to us , however ptr Or point to this space , It's dangerous ,
// therefore , Usually free After falling , Set the pointer pointing to this space to NULL Null pointer
if (ptr != NULL)
{
*ptr = 100;
}
return 0;
}
It is worth noting that :
1. The dynamic space should be actively released , And the operating system , Otherwise, it may lead to memory leakage .
2.malloc(0) It's undefined , The consequences depend on the compiler .
3. If parameters ptr The pointed space is not opened dynamically , that free The behavior of a function is undefined .
4.free(NULL); The result is that nothing happens .
5. There will also be problems if the released space is released again ., because free What is needed is the starting address of the free space , If released , This space does not belong to us , There will be the problem of wild pointer
2、calloc
calloc And malloc It's not that different , It is initialized to 0
3、realloc

realloc Used to adjust the size of the opening space
1. The space you want to expand is large enough in the back
2. Not big enough ( Already occupied ),realloc A space of sufficient size will be selected in a suitable area behind to expand
meanwhile , Will copy the previous space into this space , At the same time, the original space free fall ( At the same time, doing so will also cause some problems , For example, too much data will affect performance , The original space may be wasted )
3. meanwhile ,realloc Failure also returns null pointer NULL
When dynamic memory development fails , It's time to end the program , Because there is not enough space behind , Unable to continue program logic .
3、 ... and 、 Memory leaks
Program ( process ) Normal end , Even in the program malloc No, free, It will also be released automatically
If it doesn't end normally , The memory is not returned to the operating system , Memory may become less and less in the future
and , Some data needs long-term operation , For example, service background
There is also embedded program , There is little memory , A little leakage makes it smaller
Four 、 A brain burning problem

5、 ... and 、 5、 ... and The color spot Beautiful Of One some Scrap word
It's not easy to create , Thank you in advance for your one button three connection , I will make progress with you , Take down offer. If you think this article is helpful , Dot . Thank you for your support , Your support is my driving force !!!
边栏推荐
- Writing a new app with swift5 requires some considerations
- Wechat selection and voting of applet completion works applet graduation design (5) assignment
- 03-GuliMall 开发环境配置
- RMAN详细教程(二) —— 备份、检查、维护、恢复
- Vulnhub-dc6 learning notes
- The secret of black industry that the risk controller cannot know
- Conditional ternary operator...
- 【无标题】
- QT学习日记16——QFile文件读写
- ZYNQ PL中断脉冲多久可以被CPU捕获到
猜你喜欢
![[C language] printf formatted output and modifier summary](/img/ee/67e393f364d2565b9b7f297c80c8fb.png)
[C language] printf formatted output and modifier summary

综合评价方法

Vulnhub-DC6学习笔记

「TakinTalks」_ 故障频繁发生,如何做好系统稳定性?

ES的操作

Establishment of APP automated test framework (VII) -- airtest basic operation

STM32应用开发实践教程:基于 RS-485 总线的多机通信应用开发

21JVM(1)

【小程序】input输入框属性说明及示例(图文+代码)

【通信】【1】幅度调制,频率调制,双边带与单边带,IQ与PSK与QAM——采样一定要满足奈奎斯特定理吗
随机推荐
Failure of CUDA installation nsight visual studio edition failed
sudo 找不到命令 command not found 解决方案
小米手机安全卸载内置应用
06-GuliMall 基础CRUD功能创建
Information retrieval summit sigir2022 best paper award came out, Melbourne Institute of technology best paper, UMass University and other best short papers
CUDA and cudnn installation tutorial (super detailed) Uninstalling CUDA, installing nsight visual studio edition of CUDA fails, and there is no CUDA option in vs2019+cuda11.1 new project
Sudo cannot find the command command not found solution
Wechat selection and voting of finished works of applet graduation project (6) opening defense ppt
watch时的value问题
The secret of black industry that the risk controller cannot know
The first jd.com technology partnership conference was held, and Boyun joined hands with jd.com technology to create new digital growth in the industry
Wwdc22 - Apple privacy technology exploration
后台运行程序方法
Wechat Evaluation of applet design works Voting applet Graduation Design (5) Task Book
函数栈帧(值得收藏)
Stress testing tools (commonly used) and sendfile process
vivado里那些看不懂的约束语句
C 库函数 - sscanf()用法
Select the value with the largest proportion of a category as the unique value of the category
05 gulimall VirtualBox set fixed IP for virtual machine