Go to Post Some High School students understand safety and can follow complex instructions. - DonRotolo [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
 
 
Thread Tools Rate Thread Display Modes
Prev Previous Post   Next Post Next
  #14   Spotlight this post!  
Unread 16-05-2009, 03:05
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 Alan Anderson View Post
The efficiency is not what you suggest it is. Pointer arithmetic adds the result of a multiplication too: addition and subtraction are scaled by the size of the structure pointed to.
Quote:
Originally Posted by AustinSchuh View Post
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.
Quote:
Originally Posted by Chris27 View Post
And as I stated in my earlier post, both cases boil down to a simple lea instruction in assembly

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 }
and its assembly

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
Alternate code

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 }
its assembly

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.
Since I'm done my work for the day, I'll continue this rant. What I (and I think Austin too) am getting at is this example (in same style as Chris' examples):

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 }
That is different and removes the need for the shift left op (since as Austin said the sizeof(int) is known at compile time and can just be added). Cant do that without explicit pointers. If the multiplication by type size is bothersome you can use an unsigned int and typecast it to a pointer. Also useful if you have an array of structs and are only working with same element in each struct (you can just add the size of the struct to the pointer).

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.
__________________
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"
 


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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


All times are GMT -5. The time now is 10:59.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi