Are pointers more efficient than arrays ?

Hi there!

I've read this article:

----> http://www.cplusplus.com/doc/tutorial/pointers/

and I've got one question: are pointers more efficient than arrays ? I think they aren't beacuse we can read:

1
2
a[5] = 0;       // a [offset of 5] = 0
*(a+5) = 0;     // pointed by (a+5) = 0 


These two expressions are equivalent and valid both if a is a pointer or if a is an array.


Nevertheless, some people don't think so...

Why is that ?
K&R says they are, so as far as I'm concerned: pointers will tend to be more efficient than arrays.

But most good compilers will optimize a[5] into *(a + 5) anyway; so you're free to use arrays where necessary. Also, on most modern computers no difference can be noticed. Most CPU clocks oscillate at over 1 GHz (one thousand million oscillations per second); with the highest I've heard of being an AMD Phenom II overclocked to oscillate at 6.4 GHz (6 thousand-four hundred million oscillations per second) (definitely true) or a Pentium 3 or 4 or something overclocked to 8 (possibly not true). That being the case, I can't see it making much difference in terms of speed unless you're putting this in a nested loop or something.
The question makes no sense. It's like asking "are variables more efficient than if statements?"

Now, I assume what the question is trying to ask is "which of the following is faster?"
1
2
3
4
T array[10];
T *p=array;
array[5]=5; //a)...
p[5]=5; //...or b)? 

The answer is a). b) involves an extra level of indirection that's resolved at run time. a) has as much indirection, but the compiler can resolve it at compile time so that a) is equivalent to
1
2
T array_0,array_1, /*...*/array_9;
array_5=5;


chrisname wrote:
But most good compilers will optimize a[5] into *(a + 5) anyway
No. All compilers do that. x[y] is by definition syntactical sugar for *(x+y). An interesting side effect of this is that x[y]==y[x].
Last edited on
It is? Oh. I stand corrected, then.

An interesting side effect of this is that x[y]==y[x].

I'll have to play with that later.

Also, I think the question was "which of these is faster":
1
2
3
4
5
char* s; /* Let's assume s has a boundary of 250 (that is, s is a null-terminated string and the null is at s[249]) */
*(s + 25) = 52;
_ vs _
char s[250];
s[25] = 52


That's how I interpreted it, anyway. But according to you, helios, they're the same. I thought that was an optimization, but apparently not (and I'll take your word for it as you've got more than 5 years of programming experience on me. Also that makes sense); so in practice *(s + 25) == s[25].
Last edited on
helios wrote:
Now, I assume what the question is trying to ask is "which of the following is faster?"


Yes, it was what I had in mind :)

chrisname wrote:
But according to you, helios, they're the same.


I'm confused now. According to helios, they aren't:

helios wrote:
The answer is a)


...
Not those, I was referring to my example code, where a) would *(s + 25) = 52; and b) would be s[25] = 52. They will compile to the same thing, according to helios.
I suspect the confusion here spawns from a misunderstanding of arrays/pointers.

Note the following:

1
2
a[5] = 0;
*(a+5) = 0;


These two lines of code are exactly the same.

Also... these two lines' syntax works with arrays OR pointers. It's not like the first line is an array and the second line is a pointer. Either line could be either one. There's no way to tell.

-- That Said --

As helios said, dereferencing a pointer is ever so slightly slower (and "ever so slightly" is being generous -- the speed difference is minescule to the point where it's almost pointless to ask such questions).


Since helios already tried to explain in words... I'll try to explain in pseudo-code:

1
2
3
4
// for argument's sake, let's say 'array' is at address 0x1000
char array[16];

array[5] = 0;  // change the 5th element in the array
because the compiler knows the address of 'array', it can write to the address directly,
so it can do this

RAM[ 0x1005 ] = 0;


Now for a pointer, it's not so straightforward:
1
2
3
4
5
char array[16];
char* ptr = array;  // let's say the address of 'ptr' is 0x1010

ptr[5] = 0;
(Barring some optimizations), the compiler can't really know what address 'ptr' points to
without examining the pointer.  Therefore in order to do the 'ptr[5] = 0' bit, it must do this:

pointer_t address = RAM[ 0x1010 ];  // read 'ptr' to see what address it points to
RAM[ address + 5 ] = 0;  // then write to that address


I don't know if that helps explain it.. but it's the best I can do.
@john 891
"which of the following is faster?"
if that's what you had in mind, then i think Disch and helios hits the right spot to answer this question. therefore, the answer is "arrays if faster"

"Are pointers more efficient than arrays?, some people think pointers are, why is that?"
in this case i think the people you are referring to means that pointers is more efficient than arrays because you can declare something like this

1
2
3
int i = 0;
cin >> i;
int* p= new int[i];

in this case i is a variable. and that can't be done in arrays for it takes a constant for its length. and pointers can point to other arrays

anyone please correct me if i'm wrong

Ah yes, I always love the expression on people's faces when I write

1
2
int array[ 10 ];
5[array] = 42;


on the whiteboard and tell them that that's legal syntax.


helios is right:

 
a[5] and *(a+5) are absolutely identical.

1
2
int array[ 10 ];
5[array] = 42;


lol.., i love that code. hahaha
Disch, thanks to your pseudo-code - now I get it ;)
Topic archived. No new replies allowed.