Array indexing vs. pointers...

der Mouse mouse at mcgill-vision.UUCP
Sat Oct 1 19:54:00 AEST 1988


In article <607 at ardent.UUCP>, mec at ardent.UUCP (Michael Chastain) writes:
> 	/* Array */
> 	for ( iArg = 1; iArg < argc; iArg++ )
> 		ProcessArg( argv[iArg] );

> 	/* Pointer */
> 	while ( --argc )
> 		ProcessArg( *++argv );

> I find the first version more writeable.  More debuggable.  More
> readable.  And more likely to be correct *the first time*.

Your first two words are the most telling: "I find".

> If you had to debug this code with a typical sdb-like debugger, which
> would you rather look at?   "iArg" or "argv"?

I would generally shut off the symbolic nonsense and look at offsets
from the argument pointer.  I don't trust compilers to produce code
corresponding to what I wrote any longer.  But I would rather look at
the pointer version, if you really want to know.

> Suppose you told your boss: "my next project will be:
> 	-- 20% sooner
> 	-- 20% cleaner
> 	-- 30% less bugs
> 	--  5% larger
> 	--  5% slower (in non-real-time-critical areas)"
> What do you think he or she would say?

He probably wouldn't care.  Next question? :-)

But also, I fail to see how forcing myself to code in a style I find
unnatural[*] is going to bring any of the improvements from the above
list.  It most certainly won't bring the first two.

[*] Not to imply a value judgement; just because it's not natural for
  me doesn't mean a thing about anyone else.

This also is not to say that I don't use [] notation.  I do, sometimes.
De gustibus non est disputandum, or whatever it's supposed to be (my
knowledge of Latin is right down there with my knowledge of COBOL).

> #if	FLAME
> The last word for you efficiency geeks:
> #if	BIG_COMPUTER
> My array code vectorizes more easily than your
> pointers-that-might-be-aliased code.  So while you're saving a cycle
> here, a cycle there, I'm getting 400% performance improvements just
> by recompiling.
> #endif

Only if your compiler supports noalias or moral equivalent, to allow
you tell it that your arrays aren't aliased either.  And in that case,
I can tell it my pointers aren't aliased....

> #if	INTEL_8086
> ....a normal 32-bit pointer....

I was under the impression that a "normal" pointer on the 8086 was 16
bits.  Of course, I don't stoop that low very often[%], so I can't
claim great familiarity with it.

[%] I *do* mean to imply a value judgement here.  I think the 8086 is a
  horrible machine to have to program.

> #endif	FLAME

					der Mouse

			old: mcgill-vision!mouse
			new: mouse at larry.mcrcim.mcgill.edu



More information about the Comp.lang.c mailing list