This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: Execution • Assume the following function that adds the sum of integers in an array using array indexing. int sum(int * array, int n) { int s=0; for(int i=0; i<n; i++) { s+=array[i]; // Equivalent to //*(int*)((char*)array+i*sizeof(int)) } Using Pointers to Optimize Execution • Now the equivalent code using pointers int sum(int* array, int n) { int s=0; int *p=&array[0]; int *pend=&array[n]; while (p < pend) { s+=*p; p++; } Using Pointers to Optimize Execution • When you increment a pointer to integer it will be incremented by 4 units because sizeof(int)==4. • Using pointers is more efficient because no indexing is required and indexing require multiplication. • Note: An optimizer may substitute the multiplication by a “<<“ operator if the size is a power of two. However, the array entries may not be a power of 2 and integer multiplication may be needed....
View
Full Document
 Fall '11
 GustavoRodriguez
 int sum, Power of two, int s=0

Click to edit the document details