为什么释放calloc需要两个参数,而malloc的只有一个? [英] Why calloc takes two arguments while malloc only one?

查看:143
本文介绍了为什么释放calloc需要两个参数,而malloc的只有一个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

IMO一个就够了,为什么释放calloc 要求把它分成两个参数呢?

IMO one is enough, why does calloc require to split it into two arguments?

推荐答案

我猜想,这可能是历史和predates时代,其中C为有功能的原型。在这些时候没有原型的参数基本上必须是 INT 的typedef 为size_t 可能甚至没有没有发明。但随后 INTMAX 是你可以用的malloc 和分裂它在两人只是给你更多的灵活性和分配的最大块让你真正分配大数组。即使在那个时候有方法来获得,凡在默认情况下清零从系统大页,所以效率与其说是一个与释放calloc 比<$ C $问题C>的malloc 。

I'd guess that this is probably history and predates the times where C had prototypes for functions. At these times without a prototype the arguments basically had to be int, the typedef size_t probably wasn't even yet invented. But then INTMAX is the largest chunk you could allocate with malloc and splitting it up in two just gives you more flexibility and allows you to allocate really large arrays. Even at that times there were methods to obtain large pages from the system that where zeroed out by default, so efficiency was not so much a problem with calloc than for malloc.

如今,随着为size_t 和手头的函数原型,这是C丰富的历史只是一个每天提醒。

Nowadays, with size_t and the function prototype at hand, this is just a daily reminder of the rich history of C.

这篇关于为什么释放calloc需要两个参数,而malloc的只有一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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