当前位置:网站首页>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
边栏推荐
猜你喜欢
随机推荐
米哈游--测试开发提前批
状态机实验
分布式事务框架 seata
一文搞定 SQL Server 执行计划
RSS订阅微信公众号初探-feed43
Sqlnet. Ora file with the connection of authentication test
win10+cuda11.7+pytorch1.12.0安装
The super perfect layout has shortcut keys and background replacement
TypeScript学习
Internship: Upload method for writing excel sheet (import)
2023年航空航天、机械与机电工程国际会议(CAMME 2023)
小米--测试开发
After building the pytorch environment, the pip and conda commands cannot be used
HNUCM 2022年暑假ACM搜索专项练习
苹果对高通说:我4.45亿美元买下一个新园区,可能计划加快基带芯片自研
Go编译原理系列7(Go源码调试)
搭建好pytorch环境后,pip和conda指令不能用
LeetCode 19:删除链表的倒数第 N 个结点
YOLOv7改进之二十二:涨点神器——引入递归门控卷积(gnConv)
OpenCV 图像拼接