Which is faster, pointer access or reference access?

They should be the same (not about the same, but exactly the same) with any non-idiotic compiler. Under the hood, references are pointers (on 99% of compilers). There's no reason for any difference.

Pedantic: the second loop could be faster (probably not) because the data is in the cache already, but that's it. :)


I'm tempted to say: who cares? Any difference in speed will be negligible, and you should chose the most readable. In this particular case, I would expect to see exactly the same code generated in both case. In more complicated cases, the compiler may not be able to determine later in the loop that the pointer has not been reseated, and may have to reread it. But for this to be the case, you'd have to be doing enough other things that the difference wouldn't be measurable.


When you hesitate between two versions of the code like those in your example, you should choose the one more readable. Possible optimization of the kind you propose is supposed to be done by the compiler.

The more readable in your case is rather the version with references (actually, maybe not really more readable, but the consensus is to prefer the usage of references because pointers are more "dangerous").

But back to the effeciency: (please if someone knows assembler, better stop reading or you risk a laugh attack...) In my opinion, as the pData is alocated on the heap, the compiler will have to compile it using pointers anyway. I think that your reasoning could be kind of correct if your structure was allocated on the stack just with "Chunk data[8];". But at latest when the compiler optimizations are on the difference should be deleted anyway.


There should be no difference in code produced by any decent compiler.