|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
|
|
Thread Tools | Rate Thread | Display Modes |
|
|
|
#1
|
|||||
|
|||||
|
Re: **FIRST EMAIL**/Java and Orbit Balls
Pointer arithmetic. It can be useful if used properly. However, I wouldn't encourage students new to C++ to use pointer arithmetic on the robot, unless of course, you want them to experience the frustration of the dreaded "Seg Fault".
|
|
#2
|
|||||
|
|||||
|
Re: **FIRST EMAIL**/Java and Orbit Balls
Quote:
![]() |
|
#3
|
||||
|
||||
|
Re: **FIRST EMAIL**/Java and Orbit Balls
Quote:
Code:
char *ptr = malloc(SIZE); |
|
#4
|
|||||
|
|||||
|
Re: **FIRST EMAIL**/Java and Orbit Balls
Quote:
(sorry for the pun). The point of pointer arithmetic is to compute the address of an element (the array data structure is optional). If you can get that address by simply adding to a pointer (stored in a register) that is more efficient than computing that address by loading a symbol and adding the result of a multiplication (indexing an array using a variable).[/SIDE RANT] I actually used a bit of pointer arithmetic this year to make a simple circular buffer for Camera Report Structures. Of course I could make a circular buffer class in Java too. Again, I am not recommending pointer arithmetic or saying it's vital, I am just saying it can be useful, simple and intuitive. My real concern with Java is not the pointer stuff but rather the runtime performance. Java uses just in time (JIT) compilation and garbage collection which should add additional (non deterministic) load to the programs run (of course I need to read more about how theses features are implemented). That being said, I think additional language options are a good thing and all of my programing students are taught Java (not C) in high school for the AP exam. Last edited by The Lucas : 15-05-2009 at 18:01. |
|
#5
|
|||||
|
|||||
|
Re: **FIRST EMAIL**/Java and Orbit Balls
Quote:
|
|
#6
|
|||
|
|||
|
Re: **FIRST EMAIL**/Java and Orbit Balls
But, at least in C, the size of the structure is a known quantity at compile time, so I would imagine that most of the time that multiplication can be eliminated by the compiler. And then on top of that, sometimes the multiplication will be by a power of 2, so the compiler will use a cheap shift operation.
|
|
#7
|
|||||
|
|||||
|
Re: **FIRST EMAIL**/Java and Orbit Balls
Quote:
|
|
#8
|
||||
|
||||
|
Re: **FIRST EMAIL**/Java and Orbit Balls
Quote:
See, here is a small piece of code I just wrote. Code:
1 #include <stdlib.h>
2
3 int main()
4 {
5 int *array = malloc(10 * sizeof(int));
6 int *ptr;
7 int i;
8
9 for(i = 0; i < 10; i++)
10 ptr = array + i;
11
12 return 0;
13 }
Code:
0000000000000000 <main>: 0: 55 push %rbp 1: 48 89 e5 mov %rsp,%rbp 4: 48 83 ec 20 sub $0x20,%rsp 8: bf 28 00 00 00 mov $0x28,%edi d: e8 00 00 00 00 callq 12 <main+0x12> 12: 48 89 45 f8 mov %rax,0xfffffffffffffff8(%rbp) 16: c7 45 ec 00 00 00 00 movl $0x0,0xffffffffffffffec(%rbp) 1d: 83 7d ec 09 cmpl $0x9,0xffffffffffffffec(%rbp) 21: 7f 19 jg 3c <main+0x3c> 23: 8b 45 ec mov 0xffffffffffffffec(%rbp),%eax 26: 48 98 cltq 28: 48 c1 e0 02 shl $0x2,%rax 2c: 48 03 45 f8 add 0xfffffffffffffff8(%rbp),%rax 30: 48 89 45 f0 mov %rax,0xfffffffffffffff0(%rbp) 34: 48 8d 45 ec lea 0xffffffffffffffec(%rbp),%rax 38: ff 00 incl (%rax) 3a: eb e1 jmp 1d <main+0x1d> 3c: b8 00 00 00 00 mov $0x0,%eax 41: c9 leaveq 42: c3 retq Code:
1 #include <stdlib.h>
2
3 int main()
4 {
5 int *array = malloc(10 * sizeof(int));
6 int *ptr;
7 int i;
8
9 for(i = 0; i < 10; i++)
10 ptr = &array[i];
11
12 return 0;
13 }
Code:
0000000000000000 <main>: 0: 55 push %rbp 1: 48 89 e5 mov %rsp,%rbp 4: 48 83 ec 20 sub $0x20,%rsp 8: bf 28 00 00 00 mov $0x28,%edi d: e8 00 00 00 00 callq 12 <main+0x12> 12: 48 89 45 f8 mov %rax,0xfffffffffffffff8(%rbp) 16: c7 45 ec 00 00 00 00 movl $0x0,0xffffffffffffffec(%rbp) 1d: 83 7d ec 09 cmpl $0x9,0xffffffffffffffec(%rbp) 21: 7f 19 jg 3c <main+0x3c> 23: 8b 45 ec mov 0xffffffffffffffec(%rbp),%eax 26: 48 98 cltq 28: 48 c1 e0 02 shl $0x2,%rax 2c: 48 03 45 f8 add 0xfffffffffffffff8(%rbp),%rax 30: 48 89 45 f0 mov %rax,0xfffffffffffffff0(%rbp) 34: 48 8d 45 ec lea 0xffffffffffffffec(%rbp),%rax 38: ff 00 incl (%rax) 3a: eb e1 jmp 1d <main+0x1d> 3c: b8 00 00 00 00 mov $0x0,%eax 41: c9 leaveq 42: c3 retq Absolutely no difference whatsoever. Last edited by Chris27 : 15-05-2009 at 23:17. Reason: fixed code typo |
|
#9
|
|||||
|
|||||
|
Re: **FIRST EMAIL**/Java and Orbit Balls
Quote:
Quote:
Quote:
Code:
1 #include <stdlib.h>
2
3 int main()
4 {
5 int *array = malloc(10 * sizeof(int));
6 int *ptr = array;
7 int i;
8
9 for(i = 0; i < 10; i++)
10 ptr++;
11
12 return 0;
13 }
Plus explicit pointers make functions like swap (commonly used in bubblesort simple) simpler using C pass by reference. Again not vital, just something that gets abstracted away in a higher level language. |
|
#10
|
|||||
|
|||||
|
Re: **FIRST EMAIL**/Java and Orbit Balls
Quote:
Quote:
|
|
#11
|
|||||
|
|||||
|
Re: **FIRST EMAIL**/Java and Orbit Balls
Quote:
Quote:
Code:
.L2:
cmpl $9, -12(%ebp)
jg .L3
movl -12(%ebp), %eax
sall $2, %eax
addl -4(%ebp), %eax
movl %eax, -8(%ebp)
leal -12(%ebp), %eax
incl (%eax)
jmp .L2
.L3:
Quote:
Code:
.L2:
cmpl $9, -12(%ebp)
jg .L3
leal -8(%ebp), %eax
addl $4, (%eax)
leal -12(%ebp), %eax
incl (%eax)
jmp .L2
.L3:
Quote:
Last edited by The Lucas : 18-05-2009 at 00:00. |
|
#12
|
||||
|
||||
|
Re: **FIRST EMAIL**/Java and Orbit Balls
Quote:
Quote:
Here is swap with references: Code:
public class MyInt {
int data;
public MyInt(int x)
{
this.data = x;
}
public static void swap(MyInt a, MyInt b)
{
a.data ^= b.data;
b.data ^= a.data;
a.data ^= b.data;
}
}
Last edited by Chris27 : 18-05-2009 at 01:09. |
|
#13
|
|||||
|
|||||
|
Re: **FIRST EMAIL**/Java and Orbit Balls
You can't dereference (&) and do pointer arithmetic like that in Java.
Quote:
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. Quote:
Again, I understand both languages and the reasons why Java doesn't allow explicit pointers (source of bugs, reference counting for garbage collecting, sercurity, etc..). Lets revisit how this whole conversion happened (with me paraphrasing) Billy: Yuck Java. Pointers ftw Jared: What cant you do with Java references? Me: Pointer Arithmetic. Jared: Granted, but you better have a good reason. <Long posts involving example code by Chris, me and Alan> I thought twice before pressing Submit Reply on my first post because I knew it wouldn't be my last on this thread Programming debates are interesting (for me atleast) but time consuming. Maybe I should have just let Billy reply or learned from C++ vs LV threads.Last edited by The Lucas : 18-05-2009 at 12:23. Reason: oops quote fix |
|
#14
|
|||||
|
|||||
|
Re: **FIRST EMAIL**/Java and Orbit Balls
Quote:
Quote:
|
|
#15
|
|||||
|
|||||
|
Re: **FIRST EMAIL**/Java and Orbit Balls
Quote:
More importantly, how much does it matter in our application? What difference will it make? |
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| **FIRST EMAIL**/Java and Orbit Balls *****Orbit Ball Discussion**** | Pjohn1959 | FIRST E-Mail Blast Archive | 44 | 18-05-2009 20:19 |
| "Java, Sun SPOT and the FIRST Robotics Competition" | pogenwurst | Programming | 54 | 02-05-2009 23:37 |
| Amazing Circuit Build and Simulation Java Applet | Chris_Elston | Electrical | 2 | 26-06-2008 19:36 |
| Eclipse Plugin, Linux, and a Java Loader | shtylman | Programming | 5 | 17-01-2008 14:44 |
| Learn C, C++, and Java with new board game. | Elgin Clock | Programming | 8 | 04-11-2005 13:28 |