[英]Use of malloc in Pthreads
This is actual C code from Pthreads: 这是来自Pthreads的实际C代码:
ThreadParms *parms = NULL;
if ((parms = (ThreadParms *) malloc (sizeof (*parms))) == NULL)
{
goto FAIL0;
}
parms->tid = thread;
parms->start = start;
parms->arg = arg;
Why did they choose to malloc *parms instead of ThreadParms? 他们为什么选择malloc * parms而不是ThreadParms? It looks like it is allocating only a pointer (which would be an error), but it apparently allocating the size of the whole structure. 看起来它只分配一个指针(这将是一个错误),但它显然分配了整个结构的大小。 Is this correct? 这个对吗?
This is a common trick in C - using dereference pointer expression in place of the actual type. 这是C中的常见技巧 - 使用解除引用指针表达式代替实际类型。
The rationale is as follows: if you have 理由如下:如果有的话
some_type *some_var = malloc(sizeof(*some_var));
and then change some_type
to some_other_type
, the code would continue working fine with only one change. 然后将some_type
更改为some_other_type
,代码将继续正常工作,只有一个更改。
However, if you start with 但是,如果你开始
some_type *some_var = malloc(sizeof(some_type));
then you have to change some_type
in two places: 那么你必须在两个地方改变some_type
:
some_other_type *some_var = malloc(sizeof(some_other_type));
or your code would have an error. 或者你的代码会有错误。
It looks like it is allocating only a pointer (which would be an error) 看起来它只分配一个指针(这将是一个错误)
The asterisk makes sizeof
evaluate to the size of the entire struct
, so the code is correct. 星号使sizeof
评估整个struct
的大小,因此代码是正确的。
*parms
is of ThreadParms
type so size is OK *parms
是ThreadParms
类型,因此大小正常
It's sometimes seen as better to do like this than the old sizeof(ThreadParms)
, so if type of parms
changes the size follows (the assignment and the sizeof
statement are on the same line) 有时这被认为比旧的sizeof(ThreadParms)
更好,所以如果parms
类型改变,则大小如下(赋值和sizeof
语句在同一行)
(however that's not perfect and does not protect against a copy/paste error when allocating some other type with the same line, but it's generally better) (但这并不完美,并且在使用相同的线路分配其他类型时不能防止复制/粘贴错误,但通常更好)
Why did they choose to malloc *parms instead of ThreadParms. 他们为什么选择malloc * parms而不是ThreadParms。
It's a common practice to use so in case if the type of parms
changes in the future, then maintenance is easier. 如果parms
的类型在将来发生变化,那么使用它是一种常见的做法,那么维护就更容易了。 But using ThreadParms
would work just as well. 但是使用ThreadParms
也可以。
It looks like it is allocating only a pointer (which would be an error), but it apparently allocating the size of the whole structure. 看起来它只分配一个指针(这将是一个错误),但它显然分配了整个结构的大小。 Is this correct? 这个对吗?
No. It's actually equivalent to using sizeof(ThreadParms)
as the sizeof operator only needs the type information and it doesn't evaluate its operand (except C99 Variable Length Arrays ). 不。它实际上相当于使用sizeof(ThreadParms)
因为sizeof运算符只需要类型信息,它不会计算其操作数(C99 可变长度数组除外)。 The type of *parms
is ThreadParms
and that's all sizeof
needs to know. *parms
的类型是ThreadParms
,所有sizeof
需要知道。
Side note: the cast to ThreadParms *
is unnecessary in C as void *
can be assigned any other data pointer. 旁注:在C中不需要转换为ThreadParms *
,因为void *
可以分配任何其他数据指针。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.