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

Al Viro
2005-09-18T21:53:03Z

On Sun, Sep 18, 2005 at 10:12:25PM +0100, Al Viro wrote:

> Compound literal _is_ an object, all right. However, decision to allocate
> storage for given object is up to compiler and it's hardly something unusual.
> "Value of right-hand side is not needed to finish calculating left-hand side,
> so its storage is fair game from that point on" is absolutely normal.

BTW, for some idea of how hard does it actually blow, with
struct foo {
int a, b[100];
};
we get
void f(struct foo *p)
{
*p = (struct foo){.b[1] = 2};
}
compiled essentially to
void f(struct foo *p)
{
static struct foo hidden_const = {.b[1] = 2};
auto struct foo hidden_var;
memcpy(&hidden_var, &hidden_const, sizeof(struct foo));
memcpy(p, &hidden_var, sizeof(struct foo));
}

That's right - two memcpy back-to-back, both inserted by gcc, intermediate
object lost immediately after the second one, both source and intermediate
introduced by gcc, so it knows that there is no aliasing problems to be
dealt with. And yes, that's with optimizations turned on - -O2 and -Os
generate the same.