当前位置:网站首页>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 .
边栏推荐
- Lombok插件
- 架构设计的五个核心要素
- 得物客服一站式工作台卡顿优化之路
- Taobao store release API interface (New), Taobao oauth2.0 store commodity API interface, Taobao commodity release API interface, Taobao commodity launch API interface, a complete set of launch store i
- Mysql database learning (7) -- a brief introduction to pymysql
- JVM (19) -- bytecode and class loading (4) -- talk about class loader again
- AI人脸编辑让Lena微笑
- Mysql database learning (8) -- MySQL content supplement
- Hcip eighth operation
- Web Authentication API兼容版本信息
猜你喜欢
![Reading the paper [sensor enlarged egocentric video captioning with dynamic modal attention]](/img/db/feb719e2715c7b9c669957995e1d83.png)
Reading the paper [sensor enlarged egocentric video captioning with dynamic modal attention]

集群、分布式、微服务的区别和介绍
上海字节面试问题及薪资福利

Lombok插件

什么是消息队列?

sql查询:将下一行减去上一行,并做相应的计算

常用消息队列有哪些?

Two person game based on bevy game engine and FPGA

Web architecture design process

Zhang Ping'an: accelerate cloud digital innovation and jointly build an industrial smart ecosystem
随机推荐
JVM (19) -- bytecode and class loading (4) -- talk about class loader again
Flinksql 读写pgsql
"Multimodal" concept
Getting started with DES encryption
4. 对象映射 - Mapping.Mapster
How to get free traffic in pinduoduo new store and what links need to be optimized in order to effectively improve the free traffic in the store
The year of the tiger is coming. Come and make a wish. I heard that the wish will come true
ssm框架的简单案例
Codeforces Round #416 (Div. 2) D. Vladik and Favorite Game
张平安:加快云上数字创新,共建产业智慧生态
SQL query: subtract the previous row from the next row and make corresponding calculations
What is dependency injection (DI)
[论文阅读] Semi-supervised Left Atrium Segmentation with Mutual Consistency Training
2pc of distributed transaction solution
CentOS 7.9 installing Oracle 21C Adventures
Zero sequence aperture of leakage relay jolx-gs62 Φ one hundred
Differences and introduction of cluster, distributed and microservice
Make web content editable
集群、分布式、微服務的區別和介紹
[Oracle] simple date and time formatting and sorting problem