What are the differences between a pointer variable and a reference variable in C++? -
i know references syntactic sugar, code easier read , write.
but differences?
summary answers , links below:
- a pointer can re-assigned number of times while reference cannot re-seated after binding.
- pointers can point (
null), whereas reference refer object. - you can't take address of reference can pointers.
- there's no "reference arithmetics" (but can take address of object pointed reference , pointer arithmetics on in
&obj + 5).
to clarify misconception:
the c++ standard careful avoid dictating how compiler must implement references, every c++ compiler implements references pointers. is, declaration such as:
int &ri = i;if it's not optimized away entirely, allocates same amount of storage pointer, , places address of
istorage.
so, pointer , reference both occupy same amount of memory.
as general rule,
- use references in function parameters , return types define useful , self-documenting interfaces.
- use pointers implement algorithms , data structures.
interesting read:
- my all-time favorite c++ faq lite.
- references vs. pointers.
- an introduction references.
- references , const.
a pointer can 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, , must assigned @ initialization:
int x = 5; int y = 6; int &r = x;a pointer has own memory address , size on stack (4 bytes on x86), whereas reference shares same memory address (with original variable) takes space on stack. since reference has same address original variable itself, safe think of reference name same variable. note: pointer points can on stack or heap. ditto reference. claim in statement not pointer must point stack. pointer variable holds memory address. variable on stack. since reference has own space on stack, , since address same variable references. more on stack vs heap. implies there real address of reference compiler not tell you.
int x = 0; int &r = x; int *p = &x; int *p2 = &r; assert(p == p2);you can have pointers pointers pointers offering levels of indirection. whereas references offer 1 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 assigned
nullptrdirectly, whereas reference cannot. if try hard enough, , know how, can make address of referencenullptr. likewise, if try hard enough can have reference pointer, , reference can containnullptr.int *p = nullptr; int &r = nullptr; <--- compiling errorpointers can iterate on array, can use
++go next item pointer pointing to, ,+ 4go 5th element. no matter size object pointer points to.a pointer needs dereferenced
*access memory location points to, whereas reference can used directly. pointer class/struct uses->access it's members whereas reference uses..a pointer variable holds memory address. regardless of how reference implemented, reference has same memory address item references.
references cannot stuffed array, whereas pointers can (mentioned user @litb)
const references can bound temporaries. pointers cannot (not without indirection):
const int &x = int(12); //legal c++ int *y = &int(12); //illegal dereference temporary.this makes
const&safer use in argument lists , forth.
Comments
Post a Comment