Pointers and References

Started by
3 comments, last by Ragadast 21 years, 10 months ago
I wrote a little program to check some differences between pointers and references.
      
#include <iostream.h>

int main()
{
	int x = 50;
	int* ptr_x;
	int &ref_x = x;

	ptr_x = &x

	cout << "x: " << int(x) << endl;
	cout << "ptr_x: " << int(ptr_x) << endl;
	cout << "&x: " << int(&x) << endl;
	cout << "&ptr_x: " << int(&ptr_x) << endl;
	cout << "*ptr_x: " << int(*ptr_x) << endl;
	cout << "ref_x: " << int(ref_x) << endl;
	cout << "&ref_x: " << int(&ref_x) << endl;

	return 0;
}
    
Well, I noticed that pointers just create another variable that uses extra space to hold the value of the int that it points to, the adress and it has its own adress. On the other hand, references do not create a new variable to hold the adress and the value, so I find these are better. Is there any other difference and which is recommendable to use (or maybe both)? [edited by - Ragadast on June 15, 2002 2:55:43 PM]
Advertisement
Pointers existed in C while references did not, so C or C-style code tends to use pointers while C++ code tends to use references. While a pointer can point to NULL or be invalid, references are always valid unless you try *really* hard to pass an invalid reference to a function (which I suspect would require either assembly programming, illegal casting, or some other nasty methods). I would expect pointers and references to be identical in terms of performance, so choose whichever you like more.

Edit: Although I didn't mention it, references are (generally) implemented using pointers. Basically they are pointers with variable semantics. Which is why accessing a variable by reference is as fast as doing so by pointer (requires a defererence) and slower than accessing a 'plain' variable.

[edited by - IndirectX on June 15, 2002 3:03:22 PM]
---visit #directxdev on afternet <- not just for directx, despite the name
One downside to references is that you can''t (to my knowledge) allocate dynamic memory with them. You can only do that with pointers.

/*=========================================*/
/* Chem0sh */
/* Lead Software Engineer & Tech Support */
/* http://www.eFaces.biz */
/*=========================================*/
/*=========================================// Chem0sh// Lead Software Engineer & Tech Support// http://www.eFaces.biz=========================================*/
quote:Original post by Chem0sh
One downside to references is that you can''t (to my knowledge) allocate dynamic memory with them. You can only do that with pointers.

new returns a pointer, but that doesn''t mean you can''t store it in a reference. Watch:

  void f(int& x){ cout << x; }main() {  int& x = *new int;  x = 10;  f(x);  delete &x}  

This is probably pointless, and I''m not sure if it''s legal, but seems possible.
---visit #directxdev on afternet <- not just for directx, despite the name
You should (prefer to) use a reference when all you need is an alias to an existing object.

You must use a pointer if you need the label to have an invalid state or to be able to be reassigned to other objects.

Always prefer a reference over a pointer if it fits in your code, since references are easier to manipulate and optimize.

Don't try to overuse references though. IndirectX's post shows a nice example of when you should never use a reference (int &x = *new int). It's not safe to dereference a pointer to build a reference; pointers can be NULL or point to junk, you could end up with an (evil) invalid reference.

[edited by - HellRaider on June 15, 2002 7:12:42 PM]

This topic is closed to new replies.

Advertisement