当前位置:网站首页>C language memory function
C language memory function
2022-07-03 14:36:00 【roseisbule】
c Language has functions to operate memory , Respectively :malloc,realloc,calloc,free.
Why should there be memory functions ?
In terms of personal feelings , When developing an address book , Everyone's address book is different , This makes it difficult to determine how much memory you need to create for the structure array . If you do not use memory functions , Then simply and roughly set the structure array to be very large , It's hard not to waste memory .
malloc:
void* malloc (size_t size);
1.malloc The function is responsible for applying for a continuous space from memory , If the application is successful , Then return the address of the space , If the application fails , Then return to NULL.
2. Because I don't know what type you want , therefore malloc The return is void* The pointer to .
3. If malloc Transmitted size_t Parameter is 0, What value is returned ,c The language standard is not defined , Depends on the compiler .
calloc:
void* calloc (size_t num, size_t size);
1.calloc and malloc equally , It is also to apply for a continuous memory space , Successful application , The return address , Failure , return NULL.
2.calloc It's an application num individual size Size space .
3.calloc Function after successful application , Each byte of this space will be initialized to 0.
realloc:
void* realloc (void* ptr,size_t size);
1.realloc Used to adjust the dynamic memory size . If the rear memory is enough to accommodate the new memory size , be realloc Will be in ptr After adding , Make the memory become size size . But if it's not enough ,realloc Will find a piece of memory again , The memory address will change .
2.realloc Function if it fails , Returns the NULL, If successful, return the address . Because I don't know whether it will succeed , So it's using realloc When , Can't directly realloc The return value of is assigned to ptr, To create an intermediate pointer , If the middle pointer is not NULL, Then assign the middle pointer to ptr.
free:
void free (void* ptr);
1.free Function is used to reclaim dynamic memory .
2.c Language standards are not defined , When to ptr What should I do when the address of is not the address opened by dynamic memory , It depends on the compiler .
3. If ptr by NULL, be free Functions do not produce behavior .
边栏推荐
- 7-14 sum integer segments (C language)
- Zzuli:1042 sum of sequence 3
- DDK for XP
- 光猫超级账号密码、宽带账号密码 获取
- The mail function of LNMP environment cannot send mail
- 洛谷P4047 [JSOI2010]部落划分 题解
- Puzzle (016.4) domino effect
- Four data flows and cases of grpc
- ShowMeBug入驻腾讯会议,开启专业级技术面试时代
- Detailed explanation of four modes of distributed transaction (Seata)
猜你喜欢
How to query the baby category of tmall on Taobao
Why is this error reported when modifying records in the database
分布式事务(Seata) 四大模式详解
Tonybot humanoid robot infrared remote control play 0630
[qingniaochangping campus of Peking University] in the Internet industry, which positions are more popular as they get older?
Dllexport et dllimport
MySQL multi table query subquery
Paper sharing: generating playful palettes from images
adc128s022 ADC verilog设计实现
ShowMeBug入驻腾讯会议,开启专业级技术面试时代
随机推荐
Zzuli: cumulative sum of 1050 factorials
retrofit
Amazon, express, lazada, shopee, eBay, wish, Wal Mart, Alibaba international, meikeduo and other cross-border e-commerce platforms evaluate how Ziyang account can seize traffic by using products in th
Detailed explanation of four modes of distributed transaction (Seata)
SSH访问控制,多次失败登录即封掉IP,防止暴力破解
Bibit pharmaceutical rushed to the scientific innovation board: annual revenue of 970000, loss of 137million, proposed to raise 2billion
Thinking about the arrangement problem in the backtracking problem (leetcode questions 46 and 47)
Tiantu investment sprint Hong Kong stocks: asset management scale of 24.9 billion, invested in xiaohongshu and Naixue
分布式事务(Seata) 四大模式详解
Niuke: crossing the river
7-6 mixed type data format input
ZABBIX saves the page blank after adding calculated items
洛谷P3065 [USACO12DEC]First! G 题解
Use of form text box (I) select text
Time conversion ()
tonybot 人形机器人 首次开机 0630
Protobuf and grpc
Luogu p5536 [xr-3] core city solution
Thread. Sleep and timeunit SECONDS. The difference between sleep
Tailing rushes to the scientific and Technological Innovation Board: it plans to raise 1.3 billion, and Xiaomi Changjiang is the shareholder