gcc 2.95.2 problem: alloca done within argument list evaluation, corrupts the rest of the argument list

Etienne LORRAIN etienne_lorrain@yahoo.fr
Fri Jan 14 02:57:00 GMT 2000

> >> f (yyy((t1 = alloca(ALLOCA_SIZE),xxx(t1))), 2);
>           ^          matches               ^
> >  undefined behavior: t1 is used and changed without
> >  resynchronisation point in between.
> But there is a sequence point.  t1=alloca(...) and xxx(t1) are
> arguments or a function call, they form a single expression by means
> of the comma operator.

  The comma operator is not a sequence point - at least if it
 did not changed on the last C standart. Initially (i.e. K&R times),
 it was an "open door" to implement fine grain parallelism on
 multiprocessor architectures - but this has never been implemented.

 You can most of the time replace ";" with "," like:
 int x, y, z, x1, y1, z1;

 x = 1000 * param1,
 y = 2500 * param2,
 z = 5 * param3; /* resynchro */
 printf ("%d, %d, %d", x, y, z), /* read only use */
 x1 = fct1(x),                   /* read only use */
 y1 = fct2(x, y, z),             /* read only use */
 z1 = fct3(z); /* resynchro */

  If comercial compiler does not want to implement it (comma
 not been a sequence point), that is their problem - but that
 may reduce the optimisation which are done.

> >   Because in C, parameters are pushed from right to left,
> That's not true in general.  In fact, there's no such thing as
> `push'ing arguments in C.  But, on x86, it's true, arguments are
> usually evaluated from right to left, because arguments are passed on
> the stack.  But you already knew that :-)

  Was true for the 8 (really different) processor I used at work...
 In fact I do not know a real implementation of C without a stack,
 or how to implement varargs without a stack and pushing params
 right to left.

> Anyway, this operation does indeed invoke undefined behavior, given
> that alloca is not part of the C standard.  In any case, it can't
> (shouldn't?) generally be called within complex expressions involving
> other function calls, since, on some architectures (such as x86),
> function calling involves stack operations, and so does alloca.

  It you want your trick, use a synchronisation point, i.e. a semicolon,
 like this:
f (yyy( ({ t1 = alloca(ALLOCA_SIZE); xxx(t1) }) ), 2);
        ^^                         ^         ^^

  That is a GCC extension, see:

  Hope that helps,
Do You Yahoo!?
Achetez, vendez! À votre prix! Sur http://encheres.yahoo.fr

More information about the Gcc-bugs mailing list