+ 2

Efficiency: Pointer vs. Reference

In a much earlier discussion there was a comparison between call by value and pointers. The gist, as far as I remember, was that for small arguments like an int, a char or something like that, pointers don't make a lot of sense just for efficiency because a pointer also has a certain size, and that the advantages only come in when the size of the arg surpasses the size of its pointer. How is this for references? How do they compare to pointers regarding memory cost and speed?

10th Apr 2019, 12:40 PM
HonFu
HonFu - avatar
4 Respostas
+ 4
Really doesn't matter. The compiler is able to generate the same code, and that without optimizations on. Here, you can test for yourself: https://godbolt.org/z/MoNkwM Also, you might want to bookmark that site, it's the number 1 site to go to for these kind of questions.
10th Apr 2019, 1:14 PM
Dennis
Dennis - avatar
+ 2
Functions with parameters use pointers. It cannot be avoided
10th Apr 2019, 3:29 PM
Da2
Da2 - avatar
+ 2
Okay, guys, nice to know - so a reference is a pointer that just can't stop pointing. :) Thank you!
10th Apr 2019, 5:52 PM
HonFu
HonFu - avatar