Array indexing vs. pointers...

Griff Smith ggs at ulysses.homer.nj.att.com
Thu Oct 6 07:45:50 AEST 1988


In article <8630 at smoke.ARPA|, gwyn at smoke.ARPA (Doug Gwyn ) writes:
| In article <1700 at dataio.Data-IO.COM| bright at dataio.Data-IO.COM (Walter Bright) writes:
| |Here's some of the things I do:
| 
| I have to take exception to most of these micro-efficiency tweaks.
| Most of them have no effect on the code generated by even a moderately
| good compiler, and nearly all of them make the code more obscure.

Agreed, but a few might deserve some further comment...

| >    o	Replace stuff like,
| >		if (a)
| >			x = b;
| >		else
| >			x = c;
| >	with,
| >		x = a ? b : c;
| 
| I don't know of any compiler that wouldn't produce the same code
| for these two cases.  Write whichever is more readable in context.

When given the second definition, C++ 2.0 (beta 4) produces the
following error message if `x', `a' and `b' are of class `foo' and
`c' is a function call that returns a `foo'.

"xx.c", line 133: sorry, not implemented: temporary of class foo with destructor needed in ? expression

| >    o	Combine printfs,
| >		printf("aksdf aksjdhf kahdf jhdsfhj\n");
| >		printf(" asdkljfhkajshdf djfh kjahsdfkja h\n");
| >	Convert to,
| >		printf("aksdf aksjdhf kahdf jhdsfhj\n\
| >		 asdkljfhkajshdf djfh kjahsdfkja h\n");
| 
| Thereby introducing a bug (exercise for the student).
| The difference in time between these is negligible,
| but if you're really tweaking for efficiency puts()
| might have been better, depending on the implementation.

Sequences of printf can be harder to read, however.
The following example may show this better:

	(void) fprintf(stderr, "\
Usage:   sltread [options] input-drive-name\n\
Options: [-l] [-s] [-v] [-x] [-z] [-t tape-volume-number]\n\
         [-d dataset-name] [-n dataset-number]\n\
         [-i ignore-count] [-r record-count]\n");

I grant that this is vulnerable to code hackers and naive
pretty-printers messing up the alignment.  On the other hand, it is
easier to see how everything lines up.
-- 
Griff Smith	AT&T (Bell Laboratories), Murray Hill
Phone:		1-201-582-7736
UUCP:		{most AT&T sites}!ulysses!ggs
Internet:	ggs at ulysses.att.com



More information about the Comp.lang.c mailing list