当前位置:网站首页>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 .
边栏推荐
- tonybot 人形机器人 定距移动 代码编写玩法
- 光猫超级账号密码、宽带账号密码 获取
- Showmebug entered Tencent conference, opening the era of professional technical interview
- Doris学习笔记之数据表的创建
- 【7.3】146. LRU caching mechanism
- Find specified characters
- How to query the baby category of tmall on Taobao
- Code writing and playing method of tonybot humanoid robot at fixed distance
- Luogu p5536 [xr-3] core city solution
- 分布式事务(Seata) 四大模式详解
猜你喜欢

How to query the baby category of tmall on Taobao
![Luogu p4047 [jsoi2010] tribal division solution](/img/7f/3fab3e94abef3da1f5652db35361df.png)
Luogu p4047 [jsoi2010] tribal division solution

dllexport和dllimport

Zzuli:1053 sine function

adc128s022 ADC verilog设计实现

NFT new opportunity, multimedia NFT aggregation platform okaleido will be launched soon

To improve efficiency or increase costs, how should developers understand pair programming?

Tonybot Humanoïde Robot Infrared Remote play 0630

Detailed explanation of four modes of distributed transaction (Seata)

Happy capital new dual currency fund nearly 4billion yuan completed its first account closing
随机推荐
Tonybot humanoid robot infrared remote control play 0630
Dllexport et dllimport
Tailing rushes to the scientific and Technological Innovation Board: it plans to raise 1.3 billion, and Xiaomi Changjiang is the shareholder
7-10 stack of hats (25 points) (C language solution)
Although not necessarily the best, it must be the hardest!
Strategy, tactics (and OKR)
Protobuf and grpc
Frequently asked questions: PHP LDAP_ add(): Add: Undefined attribute type in
适用于XP的DDK
论文分享:Generating Playful Palettes from Images
1017 a divided by B (20 points)
洛谷P4047 [JSOI2010]部落划分 题解
556. The next larger element III: simple construction simulation questions
7-3 count the number of words in a line of text
7-15 calculation of PI
LNMP环境mail函数不能发送邮件解决
Understand the application scenario and implementation mechanism of differential segment
556. 下一个更大元素 III : 简单构造模拟题
Luogu p5194 [usaco05dec]scales s solution
pyQt界面制作(登录+跳转页面)