malloc和calloc在内存布局方面的区别? [英] Difference in between malloc and calloc in their memory layout?

查看:81
本文介绍了malloc和calloc在内存布局方面的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

calloc 分配num个内存块,每个内存大小:



calloc allocates num blocks of memory, each of size size:

void * calloc ( size_t num, size_t size );





为内存中的数组分配空间。为一个num元素数组分配一个内存块,每个元素都有大小字节长,并将其所有位初始化为零。



相比之下, malloc 分配一个大小合适的内存块:





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.

In contrast, malloc allocates one block of memory of size size:

void * malloc ( size_t size );





分配内存块。分配一个大小字节的内存块,返回一个指向块开头的指针。



现在我的问题是:



1.两者之间是否有任何区别(calloc的零初始化除外)..?



2. calloc()是否分配只有连续的内存或它也可以分配非连续的..?



Allocate memory block. Allocates a block of size bytes of memory, returning a pointer to the beginning of the block.

Now my questions are:

1. Is there any difference between both (except for the zero-initialization by calloc)..?

2. Does calloc() allocate only contiguous memory Or it can allocate non-contiguous also..?

推荐答案

1)没有。



2)否。它始终是连续的。
1) No.

2) No. It's always contiguous.


1。实际上,calloc()创建的内存由操作系统触摸并用零初始化。与malloc()相比,将会有一些性能,其中返回内存指针,指示分配的内存块的开始。



除非操作系统正在使用,否则分配的内存不会被触摸。



2.是分配给calloc()的内存总是由OS连续。如果需要,它会交换内存空间,以便将其分配给calloc()。



[brydon]大写和拼写修正[/ brydon]
1. Actually, the memory created by calloc () is TOUCHED by the OS and initialized with zeros. There will be some performance compared to malloc( ) where the memory pointer is being returned which indicates the start of the memory block allocated.

The memory allocated is NOT TOUCHED unless it is being used by the OS.

2. Yes the memory allocated to calloc() is always contiguous by the OS. It swaps out the memory space if required, in order to assign it to calloc().

[brydon]capitalization and spelling fixes[/brydon]


这篇关于malloc和calloc在内存布局方面的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆