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:

  1. a pointer can re-assigned number of times while reference cannot re-seated after binding.
  2. pointers can point (null), whereas reference refer object.
  3. you can't take address of reference can pointers.
  4. 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 i storage.

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:

  1. 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; 
  2. 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); 
  3. 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); 
  4. pointer can assigned nullptr directly, whereas reference cannot. if try hard enough, , know how, can make address of reference nullptr. likewise, if try hard enough can have reference pointer, , reference can contain nullptr.

    int *p = nullptr; int &r = nullptr; <--- compiling error 
  5. pointers can iterate on array, can use ++ go next item pointer pointing to, , + 4 go 5th element. no matter size object pointer points to.

  6. 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 ..

  7. a pointer variable holds memory address. regardless of how reference implemented, reference has same memory address item references.

  8. references cannot stuffed array, whereas pointers can (mentioned user @litb)

  9. 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

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -