当前位置:网站首页>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
边栏推荐
- 20年将投资美国约2000亿美元,三星电子财大气粗的样子真好看
- In V8 how arrays (with source code, picture and text easier to understand)
- 超级完美版布局有快捷键,有背景置换(解决opencv 中文路径问题)
- After building the pytorch environment, the pip and conda commands cannot be used
- Nanoprobes 棕榈酰纳米金相关说明书
- 绕任意轴旋转矩阵推导
- ros mavros stereo读取rosbag并记录IMU和图片到文件夹
- boot issue
- 越来越火的图数据库到底能做什么?
- 利用matlab求解线性优化问题【基于matlab的动力学模型学习笔记_11】
猜你喜欢

【面经】被虐了之后,我翻烂了equals源码,总结如下

JVM垃圾回收总结(未完待续)

Why Flutter Flutter of tutorials is the best choice for business?

ping数据包中的进程号

初始 List 接口
![2022-08-03:以下go语言代码输出什么?A:2;B:3;C:1;D:0。 package main import “fmt“ func main() { slice := []i](/img/a9/6de3c2bae92d09b13b1c36e01f86c2.png)
2022-08-03:以下go语言代码输出什么?A:2;B:3;C:1;D:0。 package main import “fmt“ func main() { slice := []i

手撕Nacos源码,今日撕服务端源码

The world's first mass production, with the most fixed points!How does this AVP Tier1 lead?

通过whl安装第三方包

面试必问的HashCode技术内幕
随机推荐
搭建好pytorch环境后,pip和conda指令不能用
XSLT – 服务器端概述
越来越火的图数据库到底能做什么?
学习笔记 | uiautomation(如何)实现自动化
关于mnn模型输出的数据杂乱无章问题
米哈游--测试开发提前批
【性能优化】MySQL性能优化之存储引擎调优
win10+cuda11.7+pytorch1.12.0 installation
初始 List 接口
The longest substring that cannot have repeating characters in a leetcode/substring
ros mavros stereo读取rosbag并记录IMU和图片到文件夹
ping数据包中的进程号
孙宇晨受邀参加36氪元宇宙峰会并发表主题演讲
[Miscellaneous] How to install the specified font into the computer and then use the font in the Office software?
MPLS综合实验
【详细教程】一文参透MongoDB聚合查询
POE交换机全方位解读(下)
vscode插件设置——Golang开发环境配置
Internship: Upload method for writing excel sheet (import)
After building the pytorch environment, the pip and conda commands cannot be used