当前位置:网站首页>Difference between malloc and calloc
Difference between malloc and calloc
2022-06-25 10:08:00 【Southern kingdom_ Love】
do C For so long , To understand calloc Functions are also a shame .
I have found many articles about the difference between the two on the Internet . Some even summed up the conclusions of many people . But I don't think it's very clear .
There is no need to discuss the differences between function prototypes , Everyone can see that the parameters are different . What needs to be discussed is the problems reflected in the prototype .
From the prototype ,malloc The meaning is “ Give me a size of size Continuous memory ”, and calloc It looks like it is. “ Give me a n Size is size Of memory ”.
Because this prototype . Some people say ( I don't know if the authorities say so )calloc An array of returned objects malloc Just a contiguous piece of memory .
This caused me great confusion . Why is there only one return value ? Is the first address of the array returned , What is stored in the array is allocated n Block memory address ? Don't calloc Of n Size is size Continuous memory , Here n But it may be discontinuous ? Suppose so, how to release this memory ?“n Size is size Of memory ” The sentence itself is ambiguous ( yes “ The size of a block of memory is n individual size” Well ? still “n The size of each block of block memory is size”), With these questions, I looked at it calloc How to use the requested memory free To release , The result is that malloc Just like free Once is enough . This result makes me think calloc It's a mystery .
This web site (http://www.cnblogs.com/ecizep/p/4417573.html) There is such a descriptive narration when comparing the two :
“malloc When allocating memory, a certain amount of space will be reserved to record the allocation , The more times you allocate , The more space these records take up . in addition , basis malloc Different implementation strategies ,malloc Every time you allocate , It is possible to allocate more space than is actually required . Multiple allocations can lead to many other such wastes , Of course , These are all related to malloc The realization of ”.
It is very normal to record memory usage , How to use the memory free To release ? But it is emphasized here malloc But I didn't say this calloc, Don't calloc No record ? that free How to free up the space applied for with it ?calloc What the hell is it ? It's amazing , I should see calloc Source code .
I found a paragraph from the following website calloc Source code (apple Your address is at least a little authoritative ), Although there are many ways to implement , But this code is enough to illustrate calloc What is it :
http://www.opensource.apple.com/source/gcc/gcc-5575.11/libiberty/calloc.c
Extract code such as the following :
#include "ansidecl.h"
#include <stddef.h>
/* For systems with larger pointers than ints, this must be declared. */
PTR malloc (size_t);
void bzero (PTR, size_t);
PTR
calloc (size_t nelem, size_t elsize)
{
register PTR ptr;
if (nelem == 0 || elsize == 0)
nelem = elsize = 1;
ptr = malloc (nelem * elsize);
if (ptr) bzero (ptr, nelem * elsize);
return ptr;
}
Look at this code . There are only two words that can express my mood
边栏推荐
- [MySQL learning notes 21] storage engine
- puzzle(019.2)六边锁
- 如何在Microsoft Exchange 2010中安装SSL证书
- Fluent creates, reads and writes JSON files
- CYCA少儿形体礼仪 乐清市培训成果考核圆满落幕
- Redis (II) distributed locks and redis cluster construction
- Kotlin common standard functions
- Mengyou Technology: six elements of tiktok's home page decoration, how to break ten thousand dollars in three days
- Reza RA series - development environment construction
- What functions should smart agriculture applet system design have
猜你喜欢

How to "transform" small and micro businesses (II)?
![[buuctf.reverse] 117-120](/img/6c/8a90fff2bd46f1494a9bd9c77eeafc.png)
[buuctf.reverse] 117-120

x86电脑上下载debian的arm64的包

Get started quickly with jetpack compose Technology

Encoding format for x86

Jetpack compose layout (I) - basic knowledge of layout

Methodchannel of flutter

Redis(二)分布式锁与Redis集群搭建

Redis (I) principle and basic use

2台三菱PLC走BCNetTCP协议,能否实现网口无线通讯?
随机推荐
Remove the mosaic, there's a way, attached with the running tutorial
What functions should smart agriculture applet system design have
What should be paid attention to in PMP examination?
Swift recursively queries the array for the number closest to the specified value
Shardingsphere proxy 4.1 sub database and sub table
字符串 最长公共前缀
Solution to the problem of repeated startup of esp8266
Vscode attempted to write the procedure to a pipeline that does not exist
Encoding format for x86
字符串 实现 strStr()
Unique Wulin, architecture selection manual (including PDF)
Use evo
Kotlin common standard functions
How to make a self-made installer and package the program to generate an installer
Jetpack compose layout (II) - material components and layout
ShardingSphere-Proxy 4.1 分庫分錶
51 SCM time stamp correlation function
STM32 receives data by using idle interrupt of serial port
BUG-00x bug description + resolve ways
ShardingSphere-Proxy 5.0 分库分表(一)