Re: [C, C++] "(a=b) = c;" Illegal or Undefined?
From: Jerry Coffin (jcoffin_at_taeus.com)
Date: 01/28/04
- Next message: Martijn Lievaart: "Re: [C] parsing command line switches"
- Previous message: Elephant: "Re: Format numbers"
- In reply to: Arthur J. O'Dwyer: "Re: [C, C++] "(a=b) = c;" Illegal or Undefined?"
- Next in thread: Martijn Lievaart: "Re: [C, C++] "(a=b) = c;" Illegal or Undefined?"
- Reply: Martijn Lievaart: "Re: [C, C++] "(a=b) = c;" Illegal or Undefined?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Wed, 28 Jan 2004 02:30:17 -0700
In article <Pine.LNX.4.58-035.0401271646190.15440
@unix48.andrew.cmu.edu>, ajo@nospam.andrew.cmu.edu says...
[ ... ]
> > We have NONE that determines the order of evaluation between the two
> > assignments.
>
> Yes, we do. The first assignment "*p = c;" happens *before* the
> return from f', and the second assignment "*(...)='B';" happens
> *after* the return from 'f'.
What part of the standard says that the second assignment happens after
the return from f? I've read and reread the applicable sections, and
can't find anything that even hints at such a thing.
> The assignments happen in chronological order, which is a linear
> ordering on the set of events that happen. Since the first assignment
> happens before "return p;", and "return p;" happens before the second
> assignment, we may conclude (from the transitivity property of linear
> orderings) that the first assignment happens before the second
> assignment. Q.E.D.
No -- this is just the same as something like:
i = ++i;
You can argue until you're blue in the face that the ++i has to be
evaluated before the assignment, and from a logical viewpoint that's
basically true. For better or worse, however, the standard disagrees.
> > > Thus we have, in this order:
> > > Call f.
> > > Assign 'A' to *p.
> > > Return p.
> > > Assign 'B' to *p.
> > >
> > > All perfectly well-defined, in both languages.
> >
> > OR we have:
> >
> > assign 'B' to *p
> > call f
>
> No, this ordering is patently impossible. You might as well say
> that we could have
>
> assign 'B' to *p
> assign 'A' to *p
> call f
Not true -- the ordering I gave is entirely possible, because the
standard doesn't specify a sequence point that prevents it. The
ordering you've given IS prevented by the sequence points specified in
the standard.
> The Standard explicitly tells us where the sequence points in this
> code are, and those sequence points prohibit your hypothetical
> implementation from re-arranging the code in these ways.
No, they don't. They say nothing about the relative order in which the
two assignments will be evaluated.
> > The sequence points are enough to determine that, in essence, the
> > assignment operator (or any other function) runs atomically.
>
> The C Standard does not define "atomically" except in the context
> of operations on sig_atomic_t, which is fairly obscure and IMHO
> not relevant to this discussion. <OT> And no, it's perfectly possible
> for an assignment operation (e.g., a structure assignment) to be
> interrupted by a signal or interrupt on all systems with which I
> am familiar. </OT>
First of all, since we're discussing overloaded operators, the C++
standard is the one that applies, not the C standard. Second, though it
doesn't use the term "atomically", the C++ standard guarantees that
(code will act as if) during the evaluation of one function, that other
code is not executed. I.e. code from one function will not act as if
its execution is interleaved with code from another function. This
prevents the ordering you've given above from being used. It does not,
however, specify anything about the relative order in which the
assignments must take place.
[ ... ]
> Yes, they are. That's what sequence points *do* -- they impose
> sequence on a set of events in the program. Are you *surprised* that
> the Standard requires this code to behave in the expected fashion? :-/
Having reread the applicable portions of the standard several times over
the last couple of days, I'll be more than surprised if you can actually
cite part of the standard that makes any such requirement. The standard
specifies some sequence points, but they're simply not adequate to
produce defined behavior for this code.
--
Later,
Jerry.
The universe is a figment of its own imagination.
- Next message: Martijn Lievaart: "Re: [C] parsing command line switches"
- Previous message: Elephant: "Re: Format numbers"
- In reply to: Arthur J. O'Dwyer: "Re: [C, C++] "(a=b) = c;" Illegal or Undefined?"
- Next in thread: Martijn Lievaart: "Re: [C, C++] "(a=b) = c;" Illegal or Undefined?"
- Reply: Martijn Lievaart: "Re: [C, C++] "(a=b) = c;" Illegal or Undefined?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|