当前位置:网站首页>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 人形机器人 查看端口并对应端口 0701
- Find specified characters
- Common shortcut keys in PCB
- 分布式事务(Seata) 四大模式详解
- 7-28 monkeys choose King (Joseph problem)
- 天谋科技 Timecho 完成近亿元人民币天使轮融资,打造工业物联网原生时序数据库
- 【7.3】146. LRU缓存机制
- China PETG market forecast and Strategic Research Report (2022 Edition)
- Zzuli: sum of 1051 square roots
- Strategy, tactics (and OKR)
猜你喜欢

Programming language: the essence of type system

Accelerating strategy learning using parallel differentiable simulation

Niuke: crossing the river

NPM install is stuck with various strange errors of node NPY

Tonybot humanoid robot infrared remote control play 0630

Paper sharing: generating playful palettes from images

Dllexport and dllimport

7-15 calculation of PI

Timecho of Tianmou technology completed an angel round financing of nearly 100 million yuan to create a native timing database of the industrial Internet of things

Tonybot humanoid robot starts for the first time 0630
随机推荐
Raft agreement
Adc128s022 ADC Verilog design and Implementation
7-4 BCD decryption (10 points)
tonybot 人形机器人 首次开机 0630
Code writing and playing method of tonybot humanoid robot at fixed distance
Luogu p5536 [xr-3] core city solution
Solr series of full-text search engines - basic principles of full-text search
全文检索引擎Solr系列—–全文检索基本原理
7-10 stack of hats (25 points) (C language solution)
Dllexport et dllimport
The mail function of LNMP environment cannot send mail
Plane vector addition
Sword finger offer 28 Symmetric binary tree
动态获取权限
556. The next larger element III: simple construction simulation questions
Some concepts about agile
表单文本框的使用(一) 选择文本
Common shortcut keys in PCB
洛谷P5018 [NOIP2018 普及组] 对称二叉树 题解
FPGA blocking assignment and non blocking assignment