Pointers versus References

closed account (jvqpDjzh)
Hello!

What are the differences between pointers and references? What are the advantages and disadvantages of ones rather than the others?

Thank you in advance!
One D: References are constant pointers can be constant or not.

Aceix.
closed account (z05DSL3A)
A pointer is an object that has a value, the value is the memory address of another object.

A reference is another 'name' or alias for an object.
closed account (jvqpDjzh)
mutexe (945)

If I ask this question here, is not because I haven't tried to make me a search on the web. I know how to use google ;-)
Please, don't respond if you are not a bit patient.
What are the differences between pointers and references?


There is mainly two differences other then syntax that I can think of off the top of my head. Though of course there is certain specific situations where other differences might arise.

1 - You can change what a pointer "points" at whereas with a reference you can't change what that reference is referencing. For example.

1
2
3
4
5
6
7
8
int number = 5;
int* intPointer = &number;

int numberTwo = 10;
intPointer = &numberTwo; // This is fine

int& numberReference = number;
numberReference = numberTwo; // This is assigning the value of numberTwo to number 


2 - References must reference something when constructed, meaning you can't have a null reference (Well you can in a roundabout hackish way but if you do it you deserve a good talking to ;p). Whereas with pointers the pointer doesn't need to actually point to anything, or in other words a null pointer. For example.

1
2
3
4
5
6
7
8
9
10
11
// This is fine.
int* nullPointer;

// This is not fine
int& badReference;

// This is fine
int* nullPointerTwo = nullptr;

// Also not fine.
int& badReferenceTwo = null;


Now I generally follow the rule of if I know that I possibly might need to point to multiple different objects using only one variable I will use a pointer, also if I know that there is a chance that I don't need to point to anything I will also use a pointer. For all other cases I will use references.

When to use pointers or references I think you learn from experience. Once you get more experience with them you will instantly start to recognize when one is more suited then the other.
Last edited on
closed account (jvqpDjzh)
Canis lupus

Sorry, is not a reference also the address of the object in memory, like the pointers do?
@ zwiluwu: to expand on the answer given by Canis lupus:

Pointers can change their value (in other words, they be made to point to something else).
References cannot change the variable they're referencing.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int i = 90; // define regular variable `i'
int *pi; // define pointer `pi'

int *pi = new int;
// use operator new to allocate some memory
// store the address of that memory into `pi'

delete pi; // deallocate memory
pi = &i; // make `pi' point to variable `i'

int &ri = i; // define `ri' to reference variable `i'
// ri and i are different names for the same thing
// ri cannot be made to reference something else
// (this is why it was initialized at definition) 


So considering the above, it should be clear that you can have invalid pointers more easily than you can have invalid references.
Pointers are still important tools in cases when you need to be able to change what is being pointed at (e.g. nodes pointing to other nodes in a list).
closed account (jvqpDjzh)
@CodeGazer (123)
:)
A curiosity, since when have you been studying C++ (if I not invade your privacy) ?

And what do you have really to know to consider yourself a modest C++ programmer?

What are the essential things about the language syntax and also about the rest in general ?
Last edited on
closed account (z05DSL3A)
Sorry, is not a reference also the address of the object in memory, like the pointers do?
No, you use an address to instantiate the reference but it does not 'hold' the address.
Last edited on by Canis lupus
A curiosity, since when have you been studying C++ (if I not invade your privacy) ?


Not all, I have been programming in C++ for around 3 years now.

And what do you have really to know to consider yourself a modest C++ programmer?


This is a tough question to answer actually and not sure if I can answer it. Though I will say that it is not really how much of the language syntax that you understand which makes you a good programmer. It is being able to break down a problem or objective and make code to solve it. This might sound a bit cryptic but really learning the language syntax and how it works is the easy part that anyone can really do, actually using that language to solve real problems is the hard part.
Last edited on
closed account (jvqpDjzh)
@Catfish666 (602): References cannot change the variable they're referencing.


Questions:

1. So references are constant variables? They seem to have the same behaviour..

2. But references store variables address, right? So, how do we use a reference in an expression? (sorry, to be completely sure)

3. If we use a constant reference as argument of a function we don't want that this function change the value of the referenced variable, otherwise every change to the reference is verified also on the referenced variable, right?
But we can't, as you said, make the reference "point" to another variable. This is also for contexts outside arguments of functions?

4. What are the best ways to pass a reference of a variable as argument of a function, with pointers or references? It depends as
CodeGazer
said on what we want to do...
So this rules could be ok right?
rules of CodeGazer: Now I generally follow the rule of if I know that I possibly might need to point to multiple different objects using only one variable I will use a pointer, also if I know that there is a chance that I don't need to point to anything I will also use a pointer. For all other cases I will use references.


5. If we want to pass a big object object as parameter of a function, what is the best way to do it: with pointers, references or simply pass all the object?
Last edited on
1. So references are constant variables? They seem to have the same behaviour..


References are constant variables only in that they can't change which variable they are referencing. You can change the value of the variable they are referencing if they aren't a const reference though. For example.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// This is a constant reference and the value of the variable it is referencing
// can't be changed through this reference.
int number = 10;
const int& constReference = number;

// This doesn't work.
constReference = 20;

// This is a non constant reference. The value of the variable it is referencing
// can be changed through this reference.
int numberTwo = 20;
int& reference = numberTwo;

// This works
reference = 10;


2. But references store variables address, right? So, how do we use a reference in an expression? (sorry, to be completely sure)


I'll leave this to someone else been awhile since I have read up on the inner implementation of references.


3. If we use a constant reference as argument of a function we don't want that this function change the value of the referenced variable, otherwise every change to the reference is verified also on the referenced variable, right?
But we can't, as you said, make the reference "point" to another variable. This is also for contexts outside arguments of functions?


Correct you can't make the reference point to another variable. As for changing the value see 1. above, and yes this is the same outside function parameters.

5. If we want to pass a big object object as parameter of a function, what is the best way to do it: with pointers, references or simply pass all the object?


This will really depend on what you want to do in that function, but generally most of the time you will probably pass that object in as a reference. With large objects you always want to pass in a reference or a const reference (If you don't want changes to happen) since when you pass by reference you don't have to copy all the data into the function and this decreases the overhead.
closed account (jvqpDjzh)
CodeGazer (125): since when you pass by reference you don't have to copy all the data into the function and this decreases the overhead.


What data is copied?

Canis lupus (165):
Sorry, is not a reference also the address of the object in memory, like the pointers do?
No, you use an address to instantiate the reference but it does not 'hold' the address.
Last edited on
What data is copied?

if you pass by reference no data is copied. That's why they are often used.
if you pass by reference no data is copied.

Not quite. There is data that is copied, it is that the actual memory address of the information is copied across. Because of this, references are only useful for complex types (efficiency of passing wise), as in class objects rather than ints.

2. But references store variables address, right? So, how do we use a reference in an expression? (sorry, to be completely sure)

A reference stores a variables address, yes, but internally (you can't access it or read it yourself, unlike a pointer). However, because it is constant, and you can't change it, once you have set it to point to a variable it behaves just like a normal variable, the only difference being that the same location as some variable somewhere else in your code is being accessed as well.

Look at it like this - all variables are references. The only difference is, normal variables allocate some memory for their data, and then point to it, while a reference just points to some data allocated somewhere else, but is otherwise exactly the same.
Last edited on
Not quite

oops yes. Apologies :)
closed account (jvqpDjzh)
the only difference being that the same location as some variable somewhere else in your code is being accessed as well.
Maybe it's because I can't understand completly english: you said that the same location is accessed by more than one variable, right?

Anyway, thanks, this site is really helpful, in many things, and not just with the stuff of the C++. You seem to be the guys that are going to save the world!
Yes, the same location is being accessed by more than one variable. Here is an example:
1
2
3
4
5
int num = 10;
int& ref = num;

ref = 20;
cout << num; // 20 
Topic archived. No new replies allowed.