| | |
array indexing vs pointer arithmetic
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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 7:55 am.
•
•
•
•
Array indexing has a cleaner syntax
Last edited by ddanbe; Nov 9th, 2008 at 8:28 am.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Make love, no war. Cave ab homine unius libri.
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:
C++ 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 10: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)).
C++ 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
- 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
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






