943,852 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3414
  • C++ RSS
Nov 9th, 2008
0

array indexing vs pointer arithmetic

Expand Post »
C++ Syntax (Toggle Plain Text)
  1. int x[5];
  2. x[4] = 10;
  3. *(x + 4) = 10;

Array indexing has a cleaner syntax but (as I've read) pointer arithmetic is faster. My question is, is it worth to use pointer arithmetic? Is there a noticeable difference?
Last edited by minas1; Nov 9th, 2008 at 7:55 am.
Similar Threads
Reputation Points: 13
Solved Threads: 8
Junior Poster in Training
minas1 is offline Offline
81 posts
since Nov 2008
Nov 9th, 2008
1

Re: array indexing vs pointer arithmetic

Quote ...
Array indexing has a cleaner syntax
You said it yourself! So why bother and make your life difficult. When you come back later to your code it will be more easy to understand then if you used pointer arithmetic. If you have a good C++ compiler he would translate both options the same way!
Last edited by ddanbe; Nov 9th, 2008 at 8:28 am.
Reputation Points: 2035
Solved Threads: 644
Senior Poster
ddanbe is online now Online
3,739 posts
since Oct 2008
Nov 9th, 2008
0

Re: array indexing vs pointer arithmetic

Alright, thanks!
Reputation Points: 13
Solved Threads: 8
Junior Poster in Training
minas1 is offline Offline
81 posts
since Nov 2008
Nov 9th, 2008
2

Re: array indexing vs pointer arithmetic

>but (as I've read) pointer arithmetic is faster
Prove it. In your example, the two are quite likely compiled to exactly the same machine code. Array subscripting is literally syntactic sugar for the equivalent pointer offset calculation. A more likely example is this:
C++ Syntax (Toggle Plain Text)
  1. int x[N];
  2. int *p = x;
  3.  
  4. for ( int i = 0; i < N; i++ )
  5. process ( x[i] );
  6.  
  7. for ( int i = 0; i < N; p += i )
  8. process ( *p );
The latter could very well be faster (though I would question why you're worried about such micro-optimizations) due to the fact that the body of the loop is only performing a dereference rather than an addition and a dereference.

My advice is not to worry about it. Use whichever more clearly displays your intentions.
Last edited by Narue; Nov 9th, 2008 at 10:32 am.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Nov 9th, 2008
0

Re: array indexing vs pointer arithmetic

From the C++ Standard:
Quote ...
The expression E1[E2] is identical (by definition) to *((E1)+(E2)).
Sometime pointer arithmetics code is evidently cleaner than "subsripted" one:
C++ Syntax (Toggle Plain Text)
  1. int countDigits(const char* str)
  2. {
  3. int n = 0;
  4. if (str)
  5. while (int c = *str++)
  6. if (c >= '0' && c <= '9')
  7. ++n;
  8. return n;
  9. }
Sometimes we have no choice: as usually (but not always) pointer arithmetics code is faster than "subscripted" one on embedded systems where we (often) must save on every microsecond...

It's a far-fetched problem. It's not a language problem...
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: runtime error while incresing the couter inside a loop
Next Thread in C++ Forum Timeline: C++ GUI help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC