This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

RE: Handling pre-increment and post-increment in GCC


Rupert Wood writes:
 > Manjunath B S wrote:
 > 
 > > return printf("Post increment : %d, Pre increment : %d\n", i++, ++i);
 > 
 > You should avoid this sort of thing if possible. The C standard makes no
 > guarantees about the order of evaluation in cases like this; this is
 > undefined behaviour.
 > 
 > In practice, how this is evaluated will depend on each architecture's ABI.

No, it's nothing at all to do with the ABI, and it has nothing to do
with the order in which the arguments are evaluated.  Postincrements
may be performaed at any time at all, as long as they are complete
before the next sequence point.  For example, gcc used to queue all
postincrements and emit them at the next sequence point.

Current versions of gcc rewrite everything into static single
assignment form, and they do that before generating any code.  The
translation looks like this:

  i_1 = 2;
  i_2 = i_1 + 1;
  i.10_3 = i_2;
  i_4 = i_2 + 1;
  D.2479_5 = printf (&"Post increment : %d, Pre increment : %d\n"[0], i.10_3, i_4);

 > So in summary: you'd do better to avoid this rather than to try and
 > understand this and fight against it!

That's right: this program is meaningless, and it isn't possible to
say in terms of the semantics of C what it does.

Andrew.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]