What are the differences between a pointer variable and a reference variable in C++?
2015-03-26 16:27
771 查看
Question:
I know references are syntactic sugar, so code is easier to read and write.
But what are the differences?
Summary from answers and links below:
A pointer can be re-assigned any number of times while a reference can not be re-seated after binding.
Pointers can point nowhere (
whereas reference always refer to an object.
You can't take the address of a reference like you can with pointers.
There's no "reference arithmetics" (but you can take the address of an object pointed by a reference and do pointer arithmetics on it as in
To clarify a misconception:
The C++ standard is very careful to avoid dictating how a compiler must implement references, but every C++ compiler implements references as pointers. That is, a declaration such as:
if it's not optimized away entirely, allocates
the same amount of storage as a pointer, and places the address of i into that storage.
So, a pointer and a reference both occupy the same amount of memory.
As a general rule,
Use references in function parameters and return types to define useful and self-documenting interfaces.
Use pointers to implement algorithms and data structures.
Interesting read:
My alltime favorite C++ FQA lite.
References vs. Pointers.
An Introduction to References.
References and const.
Answer:
A pointer can be re-assigned:
A reference cannot, and must be assigned at initialization:
A pointer has its own memory address and size on the stack (4 bytes on x86), whereas a reference shares the same memory address (with the original variable) but also takes up some space on the stack. Since a reference has the same address as the original variable
itself, it is safe to think of a reference as another name for the same variable. Note: What a pointer points to can be on the stack or heap. Ditto a reference. My claim in this statement is not that a pointer must point to the stack. A pointer is just a variable
that holds a memory address. This variable is on the stack. Since a reference has its own space on the stack, and since the address is the same as the variable it references. More on stack
vs heap. This implies that there is a real address of a reference that the compiler will not tell you.
You can have pointers to pointers to pointers offering extra levels of indirection. Whereas references only offer one level of indirection.
Pointer can be assigned NULL directly, whereas reference cannot. If you try hard enough, and you know how, you can make the address of a reference NULL. Likewise, if you try hard enough you can have a reference to a pointer, and then that reference can contain
NULL.
Pointers can iterate over an array, you can use
go to the next item that a pointer is pointing to, and
A pointer needs to be dereferenced with
access the memory location it points to, whereas a reference can be used directly. A pointer to a class/struct uses
access it's members whereas a reference uses a
A pointer is a variable that holds a memory address. Regardless of how a reference is implemented, a reference has the same memory address as the item it references.
References cannot be stuffed into an array, whereas pointers can be (Mentioned by user @litb)
Const references can be bound to temporaries. Pointers cannot (not without some indirection):
This makes
for use in argument lists and so forth.
I know references are syntactic sugar, so code is easier to read and write.
But what are the differences?
Summary from answers and links below:
A pointer can be re-assigned any number of times while a reference can not be re-seated after binding.
Pointers can point nowhere (
NULL),
whereas reference always refer to an object.
You can't take the address of a reference like you can with pointers.
There's no "reference arithmetics" (but you can take the address of an object pointed by a reference and do pointer arithmetics on it as in
&obj + 5).
To clarify a misconception:
The C++ standard is very careful to avoid dictating how a compiler must implement references, but every C++ compiler implements references as pointers. That is, a declaration such as:
int &ri = i;
if it's not optimized away entirely, allocates
the same amount of storage as a pointer, and places the address of i into that storage.
So, a pointer and a reference both occupy the same amount of memory.
As a general rule,
Use references in function parameters and return types to define useful and self-documenting interfaces.
Use pointers to implement algorithms and data structures.
Interesting read:
My alltime favorite C++ FQA lite.
References vs. Pointers.
An Introduction to References.
References and const.
Answer:
A pointer can be re-assigned:
int x = 5; int y = 6; int *p; p = &x; p = &y; *p = 10; assert(x == 5); assert(y == 10);
A reference cannot, and must be assigned at initialization:
int x = 5; int y = 6; int &r = x;
A pointer has its own memory address and size on the stack (4 bytes on x86), whereas a reference shares the same memory address (with the original variable) but also takes up some space on the stack. Since a reference has the same address as the original variable
itself, it is safe to think of a reference as another name for the same variable. Note: What a pointer points to can be on the stack or heap. Ditto a reference. My claim in this statement is not that a pointer must point to the stack. A pointer is just a variable
that holds a memory address. This variable is on the stack. Since a reference has its own space on the stack, and since the address is the same as the variable it references. More on stack
vs heap. This implies that there is a real address of a reference that the compiler will not tell you.
int x = 0; int &r = x; int *p = &x; int *p2 = &r; assert(p == p2);
You can have pointers to pointers to pointers offering extra levels of indirection. Whereas references only offer one level of indirection.
int x = 0; int y = 0; int *p = &x; int *q = &y; int **pp = &p; pp = &q;//*pp = q **pp = 4; assert(y == 4); assert(x == 0);
Pointer can be assigned NULL directly, whereas reference cannot. If you try hard enough, and you know how, you can make the address of a reference NULL. Likewise, if you try hard enough you can have a reference to a pointer, and then that reference can contain
NULL.
int *p = NULL; int &r = NULL; <--- compiling error
Pointers can iterate over an array, you can use
++to
go to the next item that a pointer is pointing to, and
+ 4to go to the 5th element. This is no matter what size the object is that the pointer points to.
A pointer needs to be dereferenced with
*to
access the memory location it points to, whereas a reference can be used directly. A pointer to a class/struct uses
->to
access it's members whereas a reference uses a
..
A pointer is a variable that holds a memory address. Regardless of how a reference is implemented, a reference has the same memory address as the item it references.
References cannot be stuffed into an array, whereas pointers can be (Mentioned by user @litb)
Const references can be bound to temporaries. Pointers cannot (not without some indirection):
const int &x = int(12); //legal C++ int *y = &int(12); //illegal to dereference a temporary.
This makes
const&safer
for use in argument lists and so forth.
相关文章推荐
- What are the differences between a pointer variable and a reference variable in C++?
- What exactly is the difference between “pass by reference” in C and in C++?
- Difference between pointer variable and reference variable in C++
- The differences between pointer and reference in C++
- What are the differences between struct and class in C++?
- What are the practical differences between SVD and wavelet transforms in data analysis?
- What is the difference between a soft reference and a weak reference in Java?
- what is the difference between static and normal variables in c++
- more effective c++:the difference between pointer and reference
- What is the difference between Views and Materialized Views in Oracle?
- macro与inline的区别 What is the difference between macro and inline?
- What are the difference between DDL, DML and DCL commands?
- What’s the difference between alignment, de novo assembly, and map to reference
- What is the difference between BIT and TINYINT in MySQL
- What are the difference between DDL, DML and DCL commands?
- What is the Difference Between Type and Class in
- What are the differences between LDAP and Active Directory?
- C++ Memory Management : What is the difference between malloc/free and new/delete?
- what the difference between __weak and __block reference?
- In PHP5, what is the difference between using self and $this? When is each appropriate?