当前位置:网站首页>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

原网站

版权声明
本文为[Southern kingdom_ Love]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202200545329420.html