一些POSIX的功能为什么得到一个单元号和单元尺寸参数? [英] Why do some POSIX functions get a element number and element size parameters?
问题描述
请参阅:
http://pubs.opengroup.org/onlinepubs/009695399/functions/malloc.html
malloc函数只是得到一个数组的大小来分配。
The malloc function simply gets a array size to allocate.
但是:
http://pubs.opengroup.org/onlinepubs/009695399/functions/calloc.html
和:
http://pubs.opengroup.org/onlinepubs/009695399/functions/fread.html
只是2的接收单元的单元尺寸和数量函数的例子。
Are just 2 examples of functions that receive element size and number of elements.
为什么这么差?难道不是更简单地得到一个尺寸,这就是它?
Why's that difference? Doesn't it more simple to get a size and that's it?
推荐答案
这是一个多原因的说明,但值尺寸
和 nmemb个
确有差别尽可能的返回值 FREAD(3)
和的fwrite(3)
关注。它们返回读取或写入的元素,其中每个元素的大小由大小
参数确定的数量。
This is more of a note than a reason, but the values of size
and nmemb
do make a difference as far as the return value of fread(3)
and fwrite(3)
is concerned. They return the number of elements read or written, where the size of each element is determined by the size
parameter.
POSIX还表示,下面再。 FREAD()
,
POSIX also says the following re. fread()
,
如果部分元素被读取,它的价值是不确定的。
If a partial element is read, its value is unspecified.
,所以理论上大小的值
可以让你找回太当数据量读取数据的差别不是<$ C的倍数$ C>尺寸(由于错误或文件结束 - )。
, so in theory the value of size
could make a difference for the data you get back too when the amount of data read isn't a multiple of size
(due to end-of-file or errors).
有关释放calloc()
,GNU C库(glibc的)只需乘尺寸
和 nmemb个
在一起,还检查溢出(用一招快速检测最尺寸
和 nmemb个
对,不会溢出)。下面是glibc的2.20相关code(从 __中的的malloc / malloc.c 的libc_calloc()
。 nmemb个
和尺寸
被称为 N
和 elem_size
,分别):
For calloc()
, the GNU C library (glibc) simply multiplies size
and nmemb
together, also checking for overflow (using a trick to quickly detect most size
and nmemb
pairs that won't overflow). Here's the relevant code from glibc 2.20 (from __libc_calloc()
in malloc/malloc.c. nmemb
and size
are called n
and elem_size
, respectively.):
/* size_t is unsigned so the behavior on overflow is defined. */
bytes = n * elem_size;
#define HALF_INTERNAL_SIZE_T \
(((INTERNAL_SIZE_T) 1) << (8 * sizeof (INTERNAL_SIZE_T) / 2))
if (__builtin_expect ((n | elem_size) >= HALF_INTERNAL_SIZE_T, 0))
{
if (elem_size != 0 && bytes / elem_size != n)
{
__set_errno (ENOMEM);
return 0;
}
}
N
和 elem_size
从不之后引用。 字节
代替。
n
and elem_size
are never referenced after that. bytes
is used instead.
的 FREAD()
实施( _IO_fread()
中的 libio / iofread.c )同样乘 nmemb个
和尺寸
在一起,只用它们来计算后的返回值。 (不同于释放calloc()
,它不检查溢出。)
The fread()
implementation (_IO_fread()
in libio/iofread.c) similarly multiplies nmemb
and size
together and only uses them to calculate the return value after that. (Unlike calloc()
, it does not check for overflow.)
我猜的原因有两个参数可能是历史性的。我有兴趣的答案了。也许尺寸
参数指定的每个I / O操作的块大小(或者作为提示服务)在某些点为 FREAD()
和的fwrite()
。
I'm guessing the reasons for having two parameters might be historical. I would be interested in the answer too. Perhaps the size
parameter specified the block size of each I/O operation (or served as a hint) at some point for fread()
and fwrite()
.
的更新的:这回答和评论有一些有趣的讨论重新。 释放calloc()
。
Update: This answer and the comments has some interesting discussion re. calloc()
.
这篇关于一些POSIX的功能为什么得到一个单元号和单元尺寸参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!