当前位置:网站首页>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.3】146. LRU caching mechanism
- 天谋科技 Timecho 完成近亿元人民币天使轮融资,打造工业物联网原生时序数据库
- retrofit
- Zhejiang University Edition "C language programming (4th Edition)" topic set reference ideas set
- Mysql多表查询 #子查询
- Zzuli:1054 monkeys eat peaches
- [qingniaochangping campus of Peking University] in the Internet industry, which positions are more popular as they get older?
- Zzuli:1057 prime number determination
- Zhonggan micro sprint technology innovation board: annual revenue of 240million, net loss of 17.82 million, proposed to raise 600million
- 分布式事务(Seata) 四大模式详解
猜你喜欢

To improve efficiency or increase costs, how should developers understand pair programming?
![[qingniaochangping campus of Peking University] in the Internet industry, which positions are more popular as they get older?](/img/f6/fe61c84f289f0e74a45946dac687a6.jpg)
[qingniaochangping campus of Peking University] in the Internet industry, which positions are more popular as they get older?

基因家族特征分析 - 染色体定位分析

Mysql多表查询 #子查询

dllexport和dllimport

Sub-GHz无线解决方案Z-Wave 800 系列ZG23 soc和ZGM230S模块

556. The next larger element III

天谋科技 Timecho 完成近亿元人民币天使轮融资,打造工业物联网原生时序数据库

一文了解微分段应用场景与实现机制

adc128s022 ADC verilog设计实现
随机推荐
tonybot 人形机器人 红外遥控玩法 0630
7-9 one way in, two ways out (25 points)
Zabbix添加Calculated items后保存页面成空白
Thread. Sleep and timeunit SECONDS. The difference between sleep
retrofit
adc128s022 ADC verilog设计实现
Plane vector addition
【7.3】146. LRU缓存机制
Zzuli:1041 sum of sequence 2
Detailed explanation of four modes of distributed transaction (Seata)
Frequently asked questions: PHP LDAP_ add(): Add: Undefined attribute type in
7-17 crawling worms (break exercise)
How Facebook moves instagram from AWS to its own server
7-19 check denomination (solve binary linear equation)
Zzuli:1047 logarithmic table
Programming language: the essence of type system
Use of constraintlayout
Zzuli:1055 rabbit reproduction
NPM install is stuck with various strange errors of node NPY
Table of mathematical constants by q779