简体   繁体   English

malloc和calloc分配的内存块布局的差异?

[英]Difference in memory block layout allocated by malloc and calloc?

calloc allocates num blocks of memory, each of size size : calloc分配num个内存块,每个内存块的size

void * calloc ( size_t num, size_t size );

Allocate space for array in memory Allocates a block of memory for an array of num elements, each of them size bytes long, and initializes all its bits to zero. 为内存中的数组分配空间为 num元素数组分配一个内存块,每个元素的大小都是字节长,并将其所有位初始化为零。

In contrast, malloc allocates one block of memory of size size : 与此相反, malloc分配的大小的存储器中的一个块size

void * malloc ( size_t size );

Allocate memory block Allocates a block of size bytes of memory, returning a pointer to the beginning of the block. 分配内存块分配一个大小为字节的内存块,返回指向块开头的指针。


Is there any difference between both (except for the zero-initialization by calloc )? 两者之间是否有任何区别( calloc的零初始化除外)?

What does calloc means exactly by num blocks of memory as in practice the returned memory region is contiguous as well. calloc究竟是什么意思是num块内存,因为实际上返回的内存区域也是连续的。

I believe there has to be some difference, otherwise it wouldn't make much sense to define two different interfaces for these methods? 我认为必须有一些区别,否则为这些方法定义两个不同的接口没有多大意义?

Sorry, no differences (but the memory is zeroed) 对不起,没有差异(但内存归零)

Calloc() can be useful when used with arrays, where you have two different values in hand: the size of arrays and the dimension of one single cell. 当与数组一起使用时,Calloc()非常有用,其中您有两个不同的值:数组的大小和一个单元的维度。 The returned area memory is contiguous in both cases, and they probably use the same data structures for keeping track of such area (but this is implementation dependent) 返回的区域内存在两种情况下都是连续的,并且它们可能使用相同的数据结构来跟踪这样的区域(但这取决于实现)

In practice they do the same thing. 在实践中,他们做同样的事情。 The advantage of calloc is that good implementations will perform an overflow detection when doing the multiplication needed to determine how much memory you need. calloc的优点是,在执行确定需要多少内存所需的乘法时,良好的实现将执行溢出检测。

If you do it something like this: 如果你这样做:

void *
calloc(size_t nmemb, size_t size)
{
    size_t sz = nmemb * size;
    void *res = malloc(sz);

'sz' might not end up being what you expect it to be. 'sz'可能不会像你期望的那样结束。 malloc will then allocate much less than the caller expected, but the caller can end up treating the returned area as large enough. 然后malloc将比调用者预期的分配少得多,但调用者最终可以将返回的区域视为足够大。 This leads to heap overflows will all the security implications that usually has. 这会导致堆溢出,这将通常具有所有安全隐患。

i have tried to find out how calloc works 我试图找出calloc是如何工作的

i find below code 我找到下面的代码

/* We use this function occasionally since the real implementation may
   be optimized when it can assume the memory it returns already is
   set to NUL.  */
void * weak_function
calloc (size_t nmemb, size_t size)
{   
  /* New memory from the trivial malloc above is always already cleared.
     (We make sure that's true in the rare occasion it might not be,
     by clearing memory in free, below.)  */
  size_t bytes = nmemb * size;

#define HALF_SIZE_T (((size_t) 1) << (8 * sizeof (size_t) / 2))
  if (__builtin_expect ((nmemb | size) >= HALF_SIZE_T, 0)
      && size != 0 && bytes / size != nmemb)
    return NULL;

  return malloc (bytes);
}

You can see there is no big difference between calloc and malloc. 你可以看到calloc和malloc之间没有什么大的区别。

you can brouse code of glibc at here 你可以在这里惹恼glibc的代码

http://sourceware.org/git/?p=glibc.git;a=blob;f=elf/dl-minimal.c http://sourceware.org/git/?p=glibc.git;a=blob;f=elf/dl-minimal.c

The only real difference is the 0 initialization with calloc() . 唯一真正的区别是使用calloc()初始化0。

Why calloc() works with block size and block count while malloc() doesn't, has never been clear to me. 为什么calloc()使用块大小和块计数,而malloc()没有,我从来都不清楚。

They are both exactly the same. 它们都完全一样。 Calloc wants you to specify how many blocks of a given size you want to allocate, because it initializes them with 0. Underlying structure is exactly the same. Calloc希望您指定要分配的给定大小的块数,因为它用0初始化它们。底层结构完全相同。

You can think of calloc() as being pretty much: 你可以认为calloc()几乎是:

void* calloc(size_t nmemb, size_t size)
{
  const size_t nbytes = nmemb * size;
  void *p = malloc(nbytes);
  if(p != NULL)
    memset(p, 0, nbytes);
  return p;
}

Note that it might be implemented completely differently, but it is the functional equivalent of the above. 请注意,它可能完全不同地实现,但它与上述功能相同。 There's no difference in the "internal layout" of the memory, you get a contigous block. 内存的“内部布局”没有区别,你会得到一个连续的块。

是。什么Calloc()的作用是它分配n嵌段每个尺寸的size_t size &然后大小的每个块size_t size为零。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM