当前位置:网站首页>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
边栏推荐
猜你喜欢
随机推荐
iframe通信
2023年航空航天、机械与机电工程国际会议(CAMME 2023)
Unity intercepts 3D images and the implementation of picture-in-picture PIP
带你造轮子,自定义一个随意拖拽可吸边的悬浮View组件
全面讲解 Handler机制原理解析 (小白必看)
【超详细】手把手教你搭建MongoDB集群搭建
扩展卡尔曼滤波EKF
2015年开源大事件汇总
c语言分层理解(c语言指针(上))
fsdbDump用法
leetcode/子串中不能有重复字符的最长子串
北京电竞元宇宙论坛活动顺利召开
Install third-party packages via whl
NLP resources that must be used for projects [Classified Edition]
Sqlnet. Ora file with the connection of authentication test
Talking about the future development direction of my country's industrial parks
Nanoprobes丨Nanogold-抗体和链霉亲和素偶联物
手撕Gateway源码,今日撕工作流程、负载均衡源码
After building the pytorch environment, the pip and conda commands cannot be used
面试必问的HashCode技术内幕









