Hi, I was wondering what the difference between a pointer and a reference is. I generally references when passing classes or something with a structure.
Hi, I was wondering what the difference between a pointer and a reference is. I generally references when passing classes or something with a structure.
Google "pointers vs references".
It's also clear that you don't have a C++ book, which is a condition you should remedy.
From the code generated perspective: Not a lot [except when the compiler can completely remove a reference and just access the original variable, whilst a pointer would have to go through the pointer at all times]. From the usage perspective, quite a bit. Aside from syntactical differences, references can not be changed after the first assignment (so you can't change WHICH OBJECT a reference refers to), and references are always pointing to a valid object [unless you do really weird undefined stuff].
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
rofl I have quite a few C++ books, but none clearly discuss the intricate differences, I know how to use both, I just sort of meant in terms of memory, not necessarily in terms of the language itself.
Most compilers (AFAIK) implement references in terms of pointers.
The standard says a reference may take no memory at all, however.
The conclusion is: it's implementation-defined. You'll have to look at the assembly output of your compiler to know.
oh, okay, thanks.. but how does something take no memory? does that mean it's only different in the compiler, and once compiled, a reference IS the thing it's a reference to?
"I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008
"the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010
A reference probably might only exist at code level (but then a pointer in some cases might too?).
For example, if you have
the compiler might simply translate it to:Code:int i = 5; int& ri = i ri += 5;
and there would be nothing corresponding to the reference.Code:int i = 5; i += 5;
This might work for a pointer too, but if I'm not mistaken, it can be harder for the compiler to prove that the pointer is still pointing to the same thing (a reference has to refer to the same thing).
--------
Whether you pass things via references or pointers varies among people (although there are some cases where you must use references - e.g operator overloading, copy constructor). Personally I use pointers only where I need the ability to pass NULL, or to change the pointer (e.g pointers to arrays).
Others prefer to use pointers when the function is intended to modify what is pointed to, as taking the address at the place of call would indicate that.
Code:void foo(int* n) { *n = 42; } int main() { int m; foo(&m); //clear that something's going on here: m can be modified //but int* p = &m; foo(p); //doesn't stand out particularly? }
Last edited by anon; 09-19-2008 at 08:19 AM.
I might be wrong.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
Maybe this is just proof that I'm not a C++ programmer to the soul (or is that sole?), but a little while back, I chased a bug where a member-variable was passed to another object, and later on it had changed. That was, because the other object stored a reference to the original variable. That is the kind of "hidden" thing, whilst if it had taken a pointer, I had known IMMEDIATELY that it was potentially modifying the value later on.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
I have to agree a little more with Elysia on this one. The one time that I try to steer away using references is any circumstance that by design a pointer that could potentially carry a null value is dereferenced.
Example:
And while true this is not necessarily a likely thing to occur in the real world it illistrates a problem that has many real-world faces in programming.Code:#include <cstdio> void swap(int &x, int &y) { int t = x; x = y; y = t; } int main(void) { int *l = new int; int m = 5; scanf("%d", l); swap(*l, m); return 0; }
Yes, and you can make that more complex:
could still be done without actually storing the location of the final member i anywhere, since there is little or no benefit [assuming the number of times ri is used is relatively small] to have the extra overhead of storing the location of the final member i vs. using the location directly.Code:int &ri = somestruct.a.b.i;
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
>> if it had taken a pointer, I had known IMMEDIATELY that it was potentially modifying the value later on.
This is Stroustrup's argument for using pointers for parameters that can be modified. However (and he says this as well), if you name the function and parameters properly, then it should be just as clear that the argument will change even if you use a reference.
Because of the other benefits of references, I prefer to use them and just make sure I use clear and meaningful function and parameter names (as well as comments) so that there is no hidden modification of an argument.