View Single Post
  #13   Spotlight this post!  
Unread 18-05-2009, 13:53
The Lucas's Avatar
The Lucas The Lucas is offline
CaMOElot, it is a silly place
AKA: My First Name is really "The" (or Brian)
FRC #0365 (The Miracle Workerz); FRC#1495 (AGR); FRC#4342 (Demon)
Team Role: Mentor
 
Join Date: Mar 2002
Rookie Year: 2001
Location: Dela-Where?
Posts: 1,564
The Lucas has a reputation beyond reputeThe Lucas has a reputation beyond reputeThe Lucas has a reputation beyond reputeThe Lucas has a reputation beyond reputeThe Lucas has a reputation beyond reputeThe Lucas has a reputation beyond reputeThe Lucas has a reputation beyond reputeThe Lucas has a reputation beyond reputeThe Lucas has a reputation beyond reputeThe Lucas has a reputation beyond reputeThe Lucas has a reputation beyond repute
Send a message via AIM to The Lucas
Re: **FIRST EMAIL**/Java and Orbit Balls

Quote:
Originally Posted by Greg Needel View Post
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 View Post
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 View Post
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 View Post
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 View Post
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 View Post
Code:
ptr = 365;
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 View Post
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 View Post
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
__________________
Electrical & Programming Mentor ---Team #365 "The Miracle Workerz"
Programming Mentor ---Team #4342 "Demon Robotics"
Founding Mentor --- Team #1495 Avon Grove High School
2007 CMP Chairman's Award - Thanks to all MOE members (and others) past and present who made it a reality.
Robot Inspector
"I don't think I'm ever more ''aware'' than I am right after I burn my thumb with a soldering iron"