当前位置:网站首页>Dynamic memory management
Dynamic memory management
2022-07-07 05:42:00 【hhy20020202】
Common memory development :
frequently-used int、long、char Type and output open up space , The size of the open space is fixed .
int a;// The space opened in the stack space is four bytes
char a;// The space opened in the stack space is one byte
short a;// The space opened in the stack space is two bytes
long a;// The space opened up in the stack space is eight bytes
int arr[10]={0};// Open up on the stack space 40 A contiguous space of bytes
Dynamic memory exploit function :
malloc function :void* malloc(size_t size);
malloc The basic function is to apply for continuous available space from memory , And return a pointer to this deficient space .
If the development is successful , The thief returns a pointer to the opened space .
If the development fails , Then return a null pointer , So it's using malloc Functions are generally used if Function determines the return value of the function .
int *p=(int *)malloc(40);// Open up for 10 An integer space
// Because the return type is void*, So the specific type should be set by yourself
if(p==NULL)
{
printf("%s\n",strerror(errno));
return 0;
}free function :void free(void *ptr);
free The function is used to release dynamically opened memory space .
calloc function :void *calloc(site_t num,size_t size);
calloc The function function is to num Size is size The elements of open up a space , And initialize each byte of the space to 0.
int *p=(int *)calloc(10,sizeof(int));// open up 10 A space of integer size
int *p=(int *)malcoc(40);// open up 10 A space of integer size malloc Function and calloc Function also opens up 40 Bytes of space , But the difference between the two functions is calloc The opened space will initialize each byte to 0.
So if there are requirements for the initial value of the open space , In general use malloc function , Define the initial value of space by yourself , Not too many requirements can be used calloc Function to open up space .
realloc function :void* realloc(void * ptr,size_t size);
realloc The function adjusts the memory size .
and realloc There may be two situations during capacity increase and adjustment :
1. After the original space, there is enough space to increase capacity .
2. There is not enough space to increase the capacity after the original space .

Appearance 1 when , Capacity expansion will not lead to changes in the original spatial data , And the situation 2, In case of capacity increase, find another large enough continuous space on the heap space to use , So the return value of the function is a new memory space .
When something happens 2 when , If you directly assign the increased space to the original space , Return when capacity increase fails NULL The pointer , The original data will be lost , So it's using realloc The function is to verify whether the augmented space is a null pointer , I'm doing an assignment .
边栏推荐
- Zhang Ping'an: accelerate cloud digital innovation and jointly build an industrial smart ecosystem
- Design, configuration and points for attention of network specified source multicast (SSM) simulation using OPNET
- async / await
- In memory, I moved from CSDN to blog park!
- Design, configuration and points for attention of network unicast (one server, multiple clients) simulation using OPNET
- Realize GDB remote debugging function between different network segments
- async / await
- Egr-20uscm ground fault relay
- 淘宝店铺发布API接口(新),淘宝oAuth2.0店铺商品API接口,淘宝商品发布API接口,淘宝商品上架API接口,一整套发布上架店铺接口对接分享
- Use Zhiyun reader to translate statistical genetics books
猜你喜欢

K6el-100 leakage relay

Web architecture design process
![[PM products] what is cognitive load? How to adjust cognitive load reasonably?](/img/75/2277e0c413be561ec963b44679eb75.jpg)
[PM products] what is cognitive load? How to adjust cognitive load reasonably?
![[论文阅读] A Multi-branch Hybrid Transformer Network for Corneal Endothelial Cell Segmentation](/img/f6/cd307c03ea723e1fb6a0011b37d3ef.png)
[论文阅读] A Multi-branch Hybrid Transformer Network for Corneal Endothelial Cell Segmentation

什么是依赖注入(DI)

AI人脸编辑让Lena微笑

Common skills and understanding of SQL optimization

论文阅读【Sensor-Augmented Egocentric-Video Captioning with Dynamic Modal Attention】

Use Zhiyun reader to translate statistical genetics books
![[paper reading] semi supervised left atrium segmentation with mutual consistency training](/img/d6/e6db0d76e81e49a83a30f8c1832f09.png)
[paper reading] semi supervised left atrium segmentation with mutual consistency training
随机推荐
Zhang Ping'an: accelerate cloud digital innovation and jointly build an industrial smart ecosystem
集群、分布式、微服務的區別和介紹
5阶多项式轨迹
淘寶商品詳情頁API接口、淘寶商品列錶API接口,淘寶商品銷量API接口,淘寶APP詳情API接口,淘寶詳情API接口
爬虫练习题(三)
得物客服一站式工作台卡顿优化之路
Tablayout modification of customized tab title does not take effect
"Multimodal" concept
How Alibaba cloud's DPCA architecture works | popular science diagram
Message queuing: how to ensure that messages are not lost
架构设计的五个核心要素
[论文阅读] Semi-supervised Left Atrium Segmentation with Mutual Consistency Training
上海字节面试问题及薪资福利
Intelligent annotation scheme of entity recognition based on hugging Face Pre training model: generate doccano request JSON format
Zero sequence aperture of leakage relay jolx-gs62 Φ one hundred
常用消息队列有哪些?
How does mapbox switch markup languages?
App clear data source code tracking
5. Data access - entityframework integration
淘宝店铺发布API接口(新),淘宝oAuth2.0店铺商品API接口,淘宝商品发布API接口,淘宝商品上架API接口,一整套发布上架店铺接口对接分享