当前位置:网站首页>dynamic memory two
dynamic memory two
2022-08-04 00:21:00 【iccoke】
Dynamic memory management
Then we will refer to the concept of dynamic memory - dynamic memory (Dynamic Memory), so that users can specify the amount of RAM the virtual operating system will start with, and set the System memory is maximized.
Applying for dynamic memory is applied in the heap area, and the heap area is managed by the user.
Here we introduce several functions involving dynamic memory, these functions are in the #include
malloc(), the function of this function is to apply for a contiguous memory space of a specified size
The parameter passed is void *(size_t size)
For example int *p=(int*) malloc(sizeof(int)*n)
This applies for n int-sized memory space in the heap area
As mentioned earlier, the memory in the heap area is required to be managed by the user
Release after use. If you don't release, you can't use this place again when you want to use it next time
Therefore, it is necessary to keep up with free(p), which means to release the memory space pointed to by p
At the same time, p=NULL; this step is to prevent the pointer from crashing due to multiple free accesses to addresses that are not allowed to be accessed.
memset(), the function of this function is to initialize uninitialized memory, and the incoming parameters are (void *,int value,size_t size)
It is important to note that the size passed in here is the number of bytes
For example
int *p=(int *)malloc(sizeof(int)*10);
memset(p,0,sizeof(int)*10);
Then this initializes the requested uninitialized memory.Then there is a problem here. According to the incoming parameters, we initialize the memory to be all 0, so can it be 1?
It's not possible
0, 1 are stored in memory as 00000000 and 01010101 respectively, then the corresponding 1 stored in memory is not 1.
calloc(), the function of this function is to apply for a continuous memory space with initialization, and the incoming parameter is (int value, size_t size).
int *p=(int*)calloc(0,sizeof(int)*10);
A continuous memory space with initialization is applied here
realloc(), the function of this function is to expand the memory, the incoming parameter is (void *, size_t size)
For example
int*p=(int *)malloc(sizeof(int )*10);
int *q=(int*)realloc(p,sizeof(int)*15);
This expands p from 10 ints to 15 ints
But the expansion operation involves several problems
First, the follow-up memory space is sufficient and can be expanded
Second, the heap memory is insufficient and the expansion fails
Third, the subsequent heap memory is insufficient and cannot be allocated. In this case, there will be a new solution for the heap memory. He will find a new place large enough by himself, and then copy the original value.The expansion is complete.
Let's observe the error in the following code
Example 1 is a complete process of malloc requesting memory space
Example 2 is wrong because int *ip actually operates memory on the stack, while free operates on heap memory and faces different objects, so there will be such a problem
Example 3, after releasing the requested memory and then operating the original memory, actually accesses the memory space that should not be accessed, and the pointer will crash
Example 4 is to access the memory that is not allowed to access, which will cause the program to crash
Example 5 is to use subscript access to apply for memory, but one more access is not allowed to access the memory
Example 6 is to move the pointer of the pointer, which will access the memory that is not allowed to be accessed
Example 7 If the heap memory allocated in the function body is to be used in the main program, we should return it, for example
This way you can access it correctly
If a local variable is defined in the function body, this cannot be returned, because the area of the pointer disappears at the end of the program, so it cannot be returned
The first one here can be linked to Example 7
And two or three, that is, the problem of not returning local variables.
Reference: Dynamic Memory Management of Graph Theory Education
边栏推荐
猜你喜欢
Nanoprobes 棕榈酰纳米金相关说明书
c语言分层理解(c语言操作符)
手撕Gateway源码,今日撕工作流程、负载均衡源码
因为一次bug的教训,我决定手撕Nacos源码(先撕客户端源码)
【详细教程】一文参透MongoDB聚合查询
Spinnaker调用Jenkins API 返回403错误
ping数据包中的进程号
BioVendor人Clara细胞蛋白(CC16)Elisa试剂盒检测步骤
vscode插件设置——Golang开发环境配置
Apple told Qualcomm: I bought a new campus for $445 million and may plan to speed up self-development of baseband chips
随机推荐
微服务的简单介绍
Install third-party packages via whl
身为程序员的我们如何卷死别人?破局重生。
【杂项】如何将指定字体装入电脑然后能在Office软件里使用该字体?
绕任意轴旋转矩阵推导
米哈游--测试开发提前批
JVM垃圾回收总结(未完待续)
ES6高级-迭代器与生成器的用法
RSS订阅微信公众号初探-feed43
Graph-node:创建一个新的subgraph
轻量级网络整理及其在Yolov5上的实现
Read FastDFS in one article
利用matlab求解线性优化问题【基于matlab的动力学模型学习笔记_11】
The "interaction design" battle of the smart cockpit
初始 List 接口
Salesforce's China business may see new changes, rumors may be closing
A Preliminary Study of RSS Subscription to WeChat Official Account-feed43
使用unbound在RHEL7上搭建DNS服务
手撕Nacos源码,今日撕服务端源码
因为一次bug的教训,我决定手撕Nacos源码(先撕客户端源码)