简体   繁体   English

malloc和calloc

[英]malloc and calloc

I know this question may be marked as a duplicate of difference between malloc and calloc but still i would like to ask it. 我知道这个问题可能被标记为malloc和calloc之间的区别重复,但我还是想问一下。

i know calloc initilizes the memory block,here my question is not focusussing on that part. 我知道calloc启动了内存块,这里我的问题并不是关注那个部分。

my question is 我的问题是

the definition of malloc says it allocates a block of memory of specified size. malloc的定义说它分配了一个指定大小的内存块。

and calloc says it allocates multiple block of memory ,each of the same size. 而calloc说它分配了多个内存块,每块内存大小相同。

is this allocation of one block of memory and multiple blocks of memory is a real difference between the two? 这个分配一块内存和多块内存是两者之间真正的区别吗?

because i feel we can do the same using malloc which can be done by calloc. 因为我觉得我们可以使用malloc做同样的事情。

for example : 例如 :

int *ptr;
ptr=(int *) malloc(100 * (sizeof(int)));

and

int *ptr;
ptr=(int *) calloc(100,sizeof(int));

would end up allocating 100 times the memory required by the int. 最终将分配int所需内存的100倍。

calloc fills the memory with ZERO's. calloc用ZERO填充内存。

p=calloc(n, m); 

is equivalent to 相当于

p=malloc(n*m); 
memset(p, 0, m * n);

Thus, if you intend to set your allocated memory to zero, then using malloc you have to compute n*m twice, or use a temp variable, which is what calloc does. 因此,如果您打算将分配的内存设置为零,那么使用malloc必须计算两次n*m ,或使用temp变量,这就是calloc所做的。

Edit: I've just read the ISO C standard and found that nowhere is specified that the implementation of calloc should check if n*m overflows, that is, if it exceeds the constant SIZE_MAX in the C99 standard. 编辑:我刚刚阅读了ISO C标准,发现没有指定calloc的实现应检查n*m溢出,即它是否超过C99标准中的常量SIZE_MAX

You are correct with your code examples ... the actual memory that is being pointed to by ptr is going to be the same size (ie, and array on the heap of 100 int objects). 您的代码示例是正确的... ptr指向的实际内存大小相同(即,100个int对象堆上的数组)。 As others have mentioned though, the call to calloc will actually zero-out that memory, where-as malloc will simply give you a pointer to that memory, and the memory may or may not have all zeroes in it. 正如其他人所提到的,对calloc的调用实际上会将该内存清零,因为malloc只会给你一个指向该内存的指针,而内存中可能有也可能没有全部为零。 For instance, if you get memory that had been recycled from another object, then the call to malloc will still have the values from its previous use. 例如,如果你获得了从另一个对象中回收的内存,那么对malloc的调用仍将具有之前使用的值。 Thus if you treat the memory as if it was "clean", and don't initialize it with some default values, you're going to end up with some type of unexpected behavior in your program. 因此,如果您将内存视为“干净”,并且不使用某些默认值对其进行初始化,那么您的程序中最终会出现某种类型的意外行为。

malloc不同, calloc还初始化内存块以包含零。

Yes the main difference is mentioned above. 是的,主要区别如上所述。 Also calloc() is slower than malloc() from operating system memory allocation perspective. 从操作系统内存分配的角度来看,calloc()也比malloc()慢。

The malloc() returns pointer doesn't touch the real memory until the program touches malloc(). 在程序接触malloc()之前,malloc()返回指针不会触及实际内存。 Whereas calloc() back's the memory with RAM. 而calloc()则使用RAM返回内存。

This has been mentioned previously on this site, but judging from the other answers, I think it is worth repeating; 这个已在此网站上提到,但从其他答案来看,我认为值得重复; multiplying two integers may result in overflow, and if that happens, 乘以两个整数可能会导致溢出,如果发生这种情况,

ptr = malloc(num*size);

will probably not have the desired result (and most likely result in a later segmentation fault). 可能不会有所需的结果(并且很可能导致后来的分段错误)。 For these situations, calloc(num,size) should be preferred (though you could also test for overflow before calling malloc, if the fact that calloc() initializes the newly allocated memory to zero bothers you). 对于这些情况,应该首选calloc(num,size) (尽管你也可以在调用malloc之前测试溢出,如果calloc()将新分配的内存初始化为零困扰你)。

the main difference between the two is calloc initializes the memory blocks to zero while malloc created memory blocks contains garbage value. 两者之间的主要区别是calloc将内存块初始化为零,而malloc创建的内存块包含垃圾值。

so it is more suitable to use calloc instead of malloc to avoid uncertainity in your code. 所以更适合使用calloc而不是malloc来避免代码中的不确定性。

===You can see the following difference for mallac() and calloc() function=== ===你可以看到mallac()calloc()函数的以下区别===

Initialization: 初始化:
malloc() doesn't clear and initialize the allocated memory. malloc()不清除和初始化已分配的内存。
calloc() initialize the allocated memory by zero. calloc()将分配的内存初始化为零。

Syntex: 西尔文特克斯:

void *malloc(size_t size);                   // syntex for malloc() function
void *calloc(size_t num, size_t size);       // syntex for calloc() function

// example
ptr = malloc(num*size);   // for malloc() function
ptr = calloc(num,size);   // for calloc() function

Argument: 参数:
If you consider malloc() syntax, it will take only 1 argument. 如果考虑malloc()语法,则只需要1个参数。
If you consider calloc() syntax, it will take 2 arguments. 如果考虑calloc()语法,则需要2个参数。

Manner of memory Allocation:: 内存分配方式::
malloc() function assigns memory of the desired 'size' from the available heap. malloc()函数从可用堆中分配所需“大小”的内存。
calloc() function assigns memory that is the size of what's equal to 'num *size'. calloc()函数分配的内存大小等于'num * size'。

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

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