Re: gcc-2.95 OK, gcc-{3,4}.X not OK

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 01/03/2010 04:54 AM, Andris Kalnozols wrote:

> Since multiple assignments are legal and evaluated from right to left,
> one could expect the following to work:
> 
>   pcptr->code = pcptr = nop;

I wouldn't.  :-)

> It does *not* work, however, using the gcc 3.3 or 4.3 compilers.
> To summarize:
> 
>   pcptr = pcptr->code = nop;   /* crashes with no compiler warning */
>   pcptr->code = pcptr = nop;   /* crashes with the warning:
>                                 * operation on `pcptr' may be undefined
>                                 */
>   pcptr->code = (pcptr = nop); /* same as above */
>   pcptr = nop;  pcptr->code = nop;  /* this works */
> 
> FWIW, the HP-UX 11.31 compiler warns/not warns in the same
> way but the compiled program is "luckier" at run time.
> 
> Perhaps the compiler should regard the above bootstrapping-style
> multiple assignments as ambiguous and issue the warning regardless
> of the order in which the targets appear.

Well, gcc doesn't necessarily know.  We do a fair bit of analysis in
an attempt to help the programmer, and we warn when we reasonably can,
but gcc isn't omniscient.

In this case, though, you were burnt by a pointlessly obscure coding
style, combined with a misunderstanding of the language.  If you write
the above code as

   pcptr = nop;
   pcptr->code = nop;

it'll always work, and its semantics are obvious.

Andrew.

[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux