当前位置:网站首页>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 .
边栏推荐
- puzzle(016.4)多米诺效应
- Niuke: crossing the river
- DDK for XP
- 7-16 find the set of integers that meet the given conditions
- Zzuli:1046 product of odd numbers
- Although not necessarily the best, it must be the hardest!
- Some concepts about agile
- tonybot 人形机器人 定距移动 代码编写玩法
- Luogu p4047 [jsoi2010] tribal division solution
- Happy capital new dual currency fund nearly 4billion yuan completed its first account closing
猜你喜欢

Niuke: crossing the river
![洛谷P5018 [NOIP2018 普及组] 对称二叉树 题解](/img/89/da1a3a38e02671628f385de0f30369.png)
洛谷P5018 [NOIP2018 普及组] 对称二叉树 题解

US stock listing of polar: how can the delivery of 55000 units support the valuation of more than 20billion US dollars

Tonybot humanoid robot checks the port and corresponds to port 0701

Zhonggan micro sprint technology innovation board: annual revenue of 240million, net loss of 17.82 million, proposed to raise 600million

Why is this error reported when modifying records in the database

Code writing and playing method of tonybot humanoid robot at fixed distance

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

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

亚马逊、速卖通、Lazada、Shopee、eBay、wish、沃尔玛、阿里国际、美客多等跨境电商平台,测评自养号该如何利用产品上新期抓住流量?
随机推荐
7-3 count the number of words in a line of text
7-17 crawling worms (break exercise)
SSH access control, blocking the IP when logging in repeatedly to prevent brute force cracking
Special research report on the market of lithium battery electrolyte industry in China (2022 Edition)
提高效率 Or 增加成本,开发人员应如何理解结对编程?
String substitution
Tonybot humanoid robot starts for the first time 0630
分布式事务(Seata) 四大模式详解
J-luggage lock of ICPC Shenyang station in 2021 regional games (simple code)
分布式事务(Seata) 四大模式详解
Showmebug entered Tencent conference, opening the era of professional technical interview
Luogu p5536 [xr-3] core city solution
Zabbix添加Calculated items后保存页面成空白
ZABBIX saves the page blank after adding calculated items
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
Dllexport and dllimport
Thinking about the arrangement problem in the backtracking problem (leetcode questions 46 and 47)
Understand the application scenario and implementation mechanism of differential segment
Zzuli:1044 failure rate
Zzuli:1048 factorial table