当前位置:网站首页>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
边栏推荐
- Notes on writing questions in C language -- monkeys eat peaches
- Kotlin keyword and operator
- Flutter Gaode map privacy compliance error
- Redis(二)分布式锁与Redis集群搭建
- Kotlin advanced - class
- (forwarding articles) after skipping multiple pages, shuttle returns to the first page and passes parameters
- puzzle(019.2)六边锁
- Ruiji takeout project (II)
- CYCA少儿形体礼仪 乐清市培训成果考核圆满落幕
- ShardingSphere-Proxy 4.1 分庫分錶
猜你喜欢

Mongodb's principle, basic use, clustering and partitioned clustering
![[buuctf.reverse] 117-120](/img/6c/8a90fff2bd46f1494a9bd9c77eeafc.png)
[buuctf.reverse] 117-120

Creo makes a mobius belt in the simplest way

独步武林,架构选型手册(包含 PDF)

Can two Mitsubishi PLC adopt bcnettcp protocol to realize wireless communication of network interface?

Basic usage and principle of schedulemaster distributed task scheduling center

Learning notes of rxjs takeuntil operator

Huipay international permet au commerce électronique transfrontalier de devenir une plate - forme de paiement transfrontalière conforme!

The gradle configuration supports the upgrade of 64 bit architecture of Xiaomi, oppo, vivo and other app stores

纳米数据世界杯数据接口,中超数据,体育数据比分,世界杯赛程api,足球比赛实时数据接口
随机推荐
Download the arm64 package of Debian on X86 computer
JS tool function, self encapsulating a throttling function
Remittance international empowers cross-border e-commerce: to be a compliant cross-border payment platform!
Exception: gradle task assemblydebug failed with exit code 1
Neat Syntax Design of an ETL Language (Part 2)
纳米数据世界杯数据接口,中超数据,体育数据比分,世界杯赛程api,足球比赛实时数据接口
How much does a small program cost? How much does a small program cost? It's clear at a glance
[shared farm] smart agriculture applet, customized development and secondary development of Kaiyuan source code, which is more appropriate?
clang frontend command failed with exit code 250
The problem of automatic page refresh after the flyer WebView pops up the soft keyboard
Encoding format for x86
Processing picture class library
Best producer consumer code
字符串 实现 strStr()
汇付国际为跨境电商赋能:做合规的跨境支付平台!
How to develop wechat applet? How to open a wechat store
Jetpack compose layout (III) - custom layout
How to make a self-made installer and package the program to generate an installer
Shuttle JSON, list, map inter transfer
MySQL source code reading (II) login connection debugging