Quote:
Originally Posted by Greg Needel
I split this conversation into 2 threads into a java discussion and an orbit ball one.
|
Sorry for the extra work Greg but it was bound to happen.
Quote:
Originally Posted by Alan Anderson
Right. The whole discussion here is based on the lack of pointer arithmetic in Java. But I'm still trying to find out why some think that pointer arithmetic is more efficient than using arrays.
|
It is clear I haven't been a targeting my statements well.
Alan, to show that pointer arithmetic can be more efficient than array indexing, I used ptr++ (cant use in Java) instead of array indexing in Chris' example loop. The generated assembly showed:
Quote:
Originally Posted by The Lucas
So 4 instructions are necessary for ptr = array + i; and only 2 instructions for ptr++; in this assembly language.
|
Basically when stepping though an array, you can avoid a multiply(or shift left) op that is need to multiply the index variable by the sizeof that type by simply adding the sizeof to a pointer each time. Incrementing a pointer in a loop is the only pointer arithmetic that I regularly use and the ptr can be even be used as the loop control variable eliminating i.
So basically I am saying (since my first post here) is pointer arithmetic (that can be done in C not Java) can be slightly more efficient in some situations. Nothing major that will make a significant difference in our robot code. Can we agree on that?
Quote:
Originally Posted by Alan Anderson
My understanding is that they compile to exactly the same thing, since in C a[i] is just syntactical sugar for *(a+i). I used to confuse people by writing i[a] instead, which gives exactly the same result.
|
Exactly all array indexing is pointer arithmetic. However, not all pointer arithmetic is array indexing. More on that next
Quote:
Originally Posted by Alan Anderson
Without significant context, I'm at a loss to understand why you'd want to write it. With the exception of using memory-mapped I/O, I have yet to see any examples anywhere where using absolute pointers gives any improvement in efficiency over using arrays.
|
I accidentally stuck my explanation in the next Chris quote, I fixed it, this is how it should have read
Quote:
Originally Posted by The Lucas
Array indexing requires a symbol (base of array) and a type (size of element). When an integer can be cast to a pointer, pointer arithmetic can be done without an array.
|
As you said, memory map I/O is the only use I can think of for that expression. However, that wasn't even meant to be a practical example. It was just meant provide an simple example of pointer arithmetic that didn't have a matching array index expression to refute Chris' statement:
Quote:
Originally Posted by Chris27
Array indexing is just an encapsulation for pointer arithmetic as I explained earlier. Any pointer arithmetic expression will have a matching array index expression and both of these expressions will compile to the exact same instructions.
|
Quote:
Originally Posted by Jared341
Because if you don't use pointer arithmetic, you can't be a real programmer.
(I know that's not Brian's argument, but it is a good excuse to post one of my all time favorite programming urban legends)
|
Great story
