public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: Andrew Haley <aph@redhat.com>
To: Andris Kalnozols <andris@hpl.hp.com>
Cc: gcc-help@gcc.gnu.org
Subject: Re: gcc-2.95 OK, gcc-{3,4}.X not OK
Date: Sun, 03 Jan 2010 11:17:00 -0000	[thread overview]
Message-ID: <4B407CBB.3030109@redhat.com> (raw)
In-Reply-To: <201001030454.UAA02954@nasdaq.hpl.hp.com>

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.

  reply	other threads:[~2010-01-03 11:17 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <4B3DE0EA.5060302@redhat.com>
2010-01-03  4:55 ` Andris Kalnozols
2010-01-03 11:17   ` Andrew Haley [this message]
2010-01-07 22:37     ` Andris Kalnozols
2010-01-08  7:55       ` Cedric Roux
2010-01-08 10:46       ` Andrew Haley

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4B407CBB.3030109@redhat.com \
    --to=aph@redhat.com \
    --cc=andris@hpl.hp.com \
    --cc=gcc-help@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).