Re: p = kmalloc(sizeof(*p), )

Linus Torvalds
2005-09-18T17:31:53Z

On Sun, 18 Sep 2005, Al Viro wrote:
>
> That's why you do
> *p = (struct foo){....};
> instead of
> memset(p, 0, sizeof...);
> p->... =...;

Actually, some day that migth be a good idea, but at least historically,
gcc has really really messed that kind of code up.

Last I looked, depending on what the initializer was, gcc would create a
temporary struct on the stack first, and then do a "memcpy()" of the
result. Not only does that obviously generate a lot of extra code, it also
blows your kernel stack to kingdom come.

So be careful out there, and check what code it generates first. With at
least a few versions of gcc.

(For _small_ structures it's wonderful. As far as I can tell, gcc does a
pretty good job on structs that are just a single long-word in size).

Linus