array indexing vs pointer arithmetic
Please support our C++ advertiser: Programming Forums
![]() |
C++" Syntax (Toggle Plain Text)
int x[5]; x[4] = 10; *(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.
•
•
•
•
Array indexing has a cleaner syntax
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
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
>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:
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.
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:
cplusplus Syntax (Toggle Plain Text)
int x[N]; int *p = x; for ( int i = 0; i < N; i++ ) process ( x[i] ); for ( int i = 0; i < N; p += i ) process ( *p );
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.
From the C++ Standard:
Sometime pointer arithmetics code is evidently cleaner than "subsripted" one:
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...
•
•
•
•
The expression E1[E2] is identical (by definition) to *((E1)+(E2)).
cplusplus Syntax (Toggle Plain Text)
int countDigits(const char* str) { int n = 0; if (str) while (int c = *str++) if (c >= '0' && c <= '9') ++n; return n; }
It's a far-fetched problem. It's not a language problem...
![]() |
Similar Threads
Other Threads in the C++ Forum
- Differences Between Java and C/C++ (C++)
- Problem of sorting words of each string using pointers (C++)
Other Threads in the C++ Forum
- Previous Thread: runtime error while incresing the couter inside a loop
- Next Thread: C++ GUI help
•
•
•
•
Views: 925 | Replies: 4 | Currently Viewing: 1 (0 members and 1 guests)






Linear Mode