RSS Forums RSS

array indexing vs pointer arithmetic

Please support our C++ advertiser: Programming Forums
Reply
Posts: 79
Reputation: minas1 is an unknown quantity at this point 
Solved Threads: 7
minas1's Avatar
minas1 minas1 is offline Offline
Junior Poster in Training

array indexing vs pointer arithmetic

  #1  
Nov 9th, 2008
  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 6:55 am.
AddThis Social Bookmark Button
Reply With Quote  
Posts: 1,223
Reputation: ddanbe is just really nice ddanbe is just really nice ddanbe is just really nice ddanbe is just really nice 
Solved Threads: 163
ddanbe's Avatar
ddanbe ddanbe is online now Online
Nearly a Posting Virtuoso

Re: array indexing vs pointer arithmetic

  #2  
Nov 9th, 2008
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 7:28 am.
"If you judge people, you have no time to love them." Mother Teresa
Make love, no war. Cave ab homine unius libri.
First rule of debugging: "If you get a different error message, you're making progress."
Danny
Reply With Quote  
Posts: 79
Reputation: minas1 is an unknown quantity at this point 
Solved Threads: 7
minas1's Avatar
minas1 minas1 is offline Offline
Junior Poster in Training

Re: array indexing vs pointer arithmetic

  #3  
Nov 9th, 2008
Alright, thanks!
Reply With Quote  
Posts: 7,460
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 676
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: array indexing vs pointer arithmetic

  #4  
Nov 9th, 2008
>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:
  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 9:32 am.
I'm here to prove you wrong.
Reply With Quote  
Posts: 2,000
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 331
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: array indexing vs pointer arithmetic

  #5  
Nov 9th, 2008
From the C++ Standard:
The expression E1[E2] is identical (by definition) to *((E1)+(E2)).
Sometime pointer arithmetics code is evidently cleaner than "subsripted" one:
  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...
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.



Views: 925 | Replies: 4 | Currently Viewing: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 2:49 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC