Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP
Path: utzoo!utgpu!water!watmath!clyde!cbosgd!ihnp4!ptsfa!ames!oliveb!sun!gorodish!guy
From: guy@gorodish.UUCP
Newsgroups: comp.lang.c
Subject: Re: Style [++i vs i++]
Message-ID: <22983@sun.uucp>
Date: Wed, 8-Jul-87 17:38:20 EDT
Article-I.D.: sun.22983
Posted: Wed Jul  8 17:38:20 1987
Date-Received: Sat, 11-Jul-87 13:59:11 EDT
References: <8213@brl-adm.ARPA>
Sender: news@sun.uucp
Lines: 37

> Actually, I once used a compiler (I don't remember which;  maybe it was the
> os9 level II 6809 compiler) where "i++" and "++i" compiled into slightly
> different code.  Apparently the instruction set had a "post-increment" and
> "pre-decrement" addressing mode (macro-11 has this too, unless I'm mistaken).
> So, if you used pre-auto-decrement ("--i"), or post-auto-increment ("i++"),
> you had the advantage of this addressing mode.

If "i" is of an *integral* type, and its value isn't being used
(i.e., it's being used in a statement like

	i++;	/* just increment "i", don't use the old or new value */

) the compiler isn't very likely to use the addressing mode, since most
instructions using addressing modes tend to reference what the
register in question points to, and "i" isn't likely to point to
something interesting (no, you probably don't want to try an
instruction like "tst", either, since "i" may be "pointing" to a
location that causes a fault when referenced - or worse).

As such, in most if not all cases, if "i" is of an integral type,
the compiled code for

	i++;

and

	++i;

should be equally good (it certainly is in our compiler).

The same might arguably be true even if "i" is of a pointer type,
since neither of those statements refer to what "i" points to, and as
such shouldn't be compiled into code that uses those addressing
modes.
	Guy Harris
	{ihnp4, decvax, seismo, decwrl, ...}!sun!guy
	guy@sun.com