Re: is this valid c or a compiler bug?
From: Dan Pop (Dan.Pop_at_cern.ch)
Date: 02/27/04
- Next message: Dan Pop: "Re: unsigned short;"
- Previous message: lallous: "Re: tricky do-while"
- In reply to: Keith Vetter: "is this valid c or a compiler bug?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 27 Feb 2004 13:55:09 GMT
In <b811ab79.0402261225.51c1430d@posting.google.com> keithv@clover.net (Keith Vetter) writes:
>After a long debugging session I tracked down a
>bug to the following line of code:
>
> np = &nl[alloc_memory()]; /* nl & np are NODE * */
>
>The problem is that nl is volatile--alloc_memory() may
>change it (via realloc). The AIX compiler fails--it grabs
>the value of nl before the call to alloc_memory(). The
>Windows VC 6.0 compiler handles it correctly.
>
>So is this a compiler bug, programmer error or ambiguous c?
Programmer error, if the function call was supposed to be evaluated
before nl. When the language doesn't specify an evaluation order
(and it seldom does), it is programmer's responsibility to enforce the
desired evaluation order, by decomposing the expression into
subexpressions:
tmp = alloc_memory();
np = nl + tmp;
Now, you are guaranteed that the alloc_memory function call will be
evaluated before nl, because the end of the first expression acts as a
sequence point.
The volatility of nl just doesn't make any difference to this discussion:
it means that nl *must* be evaluated (instead of reusing an old copy of
it that might still be available in a register), but the exact moment nl
is evaluated is still unspecified (both before and after the alloc_memory
function calls are valid options for the compiler).
Dan
-- Dan Pop DESY Zeuthen, RZ group Email: Dan.Pop@ifh.de
- Next message: Dan Pop: "Re: unsigned short;"
- Previous message: lallous: "Re: tricky do-while"
- In reply to: Keith Vetter: "is this valid c or a compiler bug?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|