linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* pointer arithmetic vs. arrays
@ 2006-02-26  2:05 James Colannino
  2006-02-26 15:29 ` Glynn Clements
  0 siblings, 1 reply; 2+ messages in thread
From: James Colannino @ 2006-02-26  2:05 UTC (permalink / raw)
  To: linux-c-programming

Hey everyone.  I've heard (and have also read in "The C Programming 
Language") that pointer arithmetic in place of arrays is, in general, 
faster.  However, I've also heard that modern compilers automatically 
make optimizations that account for this difference.

In everyone's opinion, taking both readability and issues of speed into 
account, what is better to use?  I'd imagine this would depend on the 
situation.  Just trying to pick up better coding habits as I learn :)

James

-- 
My blog: http://www.crazydrclaw.com/
My homepage: http://james.colannino.org/

"If Carpenters made houses the way programmers design programs, the first woodpecker to come along would destroy all of civilization." --Computer Proverb


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: pointer arithmetic vs. arrays
  2006-02-26  2:05 pointer arithmetic vs. arrays James Colannino
@ 2006-02-26 15:29 ` Glynn Clements
  0 siblings, 0 replies; 2+ messages in thread
From: Glynn Clements @ 2006-02-26 15:29 UTC (permalink / raw)
  To: James Colannino; +Cc: linux-c-programming


James Colannino wrote:

> Hey everyone.  I've heard (and have also read in "The C Programming 
> Language") that pointer arithmetic in place of arrays is, in general, 
> faster.  However, I've also heard that modern compilers automatically 
> make optimizations that account for this difference.
> 
> In everyone's opinion, taking both readability and issues of speed into 
> account, what is better to use?  I'd imagine this would depend on the 
> situation.  Just trying to pick up better coding habits as I learn :)

In most cases, I'd recommend array indexing.

If something can reasonably be expressed in either form, array
indexing is usually clearer (both to read and to debug), and compilers
which don't optimise array access are rare enough not to be worth
worrying about.

-- 
Glynn Clements <glynn@gclements.plus.com>

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2006-02-26 15:29 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2006-02-26  2:05 pointer arithmetic vs. arrays James Colannino
2006-02-26 15:29 ` Glynn Clements

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).