Difference between Pointers and Reference in C++

Discussion in 'C++' started by Shishir191, Jul 27, 2007.

  1. Shishir191

    Shishir191 New Member

    Joined:
    Jul 24, 2007
    Messages:
    27
    Likes Received:
    1
    Trophy Points:
    0
    Occupation:
    Software Engineer
    Location:
    Delhi
    Pointers and Reference looks similar but there are some difference between both of them.

    POINTER


    1. Its not necessary to initialize the pointer at the time of declaration. Like
      Code:
         
      int a = 10;
      int *P = &a; //It is not necessary 
      
      Another way is :
      Code:
      int a  = 10;
      int *P;
      P =  &a;  
      
    2. You can create the array of Pointer.
    3. You can assign NULL to the pointer like

      Code:
      int *P = NULL; //Valid
    4. You can use pointer to pointer.

    REFERENCE


    1. Its necessary to initialize the Reference at the time of declaration. Like
      Code:
      int &a = 10;
      int &a;   //Error here but not in case of Pointer.
    2. You can not create the Array of reference.
    3. You can not assign NULL to the reference like

      Code:
      int &a = NULL; //Error
    4. You can not use reference to reference.
     
    leila likes this.
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Nice differences.
     
  3. Shishir191

    Shishir191 New Member

    Joined:
    Jul 24, 2007
    Messages:
    27
    Likes Received:
    1
    Trophy Points:
    0
    Occupation:
    Software Engineer
    Location:
    Delhi
  4. kush_2207

    kush_2207 New Member

    Joined:
    Jun 26, 2007
    Messages:
    49
    Likes Received:
    1
    Trophy Points:
    0
    good one.....but what would you say if i write in .NET or even in JAVA
    FileStream fs; // (or using any other class)
    What is FileStream -> a pointer or a reference ?
    Can you elaborate it for other platforms ?
     
  5. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    In Java or C# everything is reference and there is nothing known as pointer.
     
  6. kush_2207

    kush_2207 New Member

    Joined:
    Jun 26, 2007
    Messages:
    49
    Likes Received:
    1
    Trophy Points:
    0
    Thank you sir.
     
  7. kaustubh

    kaustubh New Member

    Joined:
    Aug 13, 2007
    Messages:
    25
    Likes Received:
    0
    Trophy Points:
    0
    Reference is the other name given to the same variable. Pointer is new variable created which can contain address of another variable.

    Try an experiment

    Code:
    # include<iostream>
    using namespace std;
    int main()
    {
    
    int i = 90; // variable
    int &j = i;  // reference 
    int *P;   //  pointer
    P =&i;
    
    cout<<endl<< "address of variable :i" <<&i;
    
    cout<< endl << "address of reference: j" <<&j;
    cout<<endl<<"address of pointer : P"<<&P;
    return 0;
    }
    OUTPUT:
     address of variable i: i0012FEDC
    address of reference j: 0012FEDC
    address of pointer p: 0012FEE0
    
    so you see the address of i and reference j is same , address of pointer P is different.
     
  8. shal

    shal New Member

    Joined:
    Sep 8, 2007
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    nice one
     
  9. zaka_d

    zaka_d New Member

    Joined:
    Sep 10, 2007
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    In C++ a reference variable is internally implemented as a constant pointer, and that is why it is necessary to initiallize a reference variable during declaration... as it is treated as a constant variable (pointer) internally.
     
  10. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    That is incorrect, zaka. A reference is a second name (alias) for an object. You can't name something that doesn't exist, therefore you have to specify the object being aliased when you declare the reference. When a reference is passed as an argument, a copy of the objects address is passed. This is one fewer levels of indirection than when a pointer to the object is passed.
     
  11. pritish

    pritish New Member

    Joined:
    Sep 4, 2007
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    pointer is variable which holds the address of another variable ,another function and incomplete in C.
    incomplete means ----------void *p;
    it's a generic pionter ,we can not allocate memory by dynamically with this .
    size of pointer is depending on the memory model.
     
  12. Kailash

    Kailash New Member

    Joined:
    Jul 24, 2007
    Messages:
    14
    Likes Received:
    0
    Trophy Points:
    0
    very appropriately describe the difference between pointer and reference. I find it very valuable
     
  13. sashimi

    sashimi New Member

    Joined:
    Nov 22, 2007
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    In C#, the object of a Class

    1. is a reference ? or
    2. is a STACK to HEAP pointer ?

    the objects shouldn't go into the HEAP ? :cool: if they are references of the class instances.
     
  14. sreeramu

    sreeramu New Member

    Joined:
    Oct 18, 2007
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Thank you good difference...
     
  15. sreeja

    sreeja New Member

    Joined:
    Nov 13, 2007
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    0
    pointer is not necessary to initialize the pointer at the time of declaration and reference to initialize the Reference at the time of declaration .Ok thank you.
     
  16. Shishir191

    Shishir191 New Member

    Joined:
    Jul 24, 2007
    Messages:
    27
    Likes Received:
    1
    Trophy Points:
    0
    Occupation:
    Software Engineer
    Location:
    Delhi
    Good
     
  17. oogabooga

    oogabooga New Member

    Joined:
    Jan 9, 2008
    Messages:
    115
    Likes Received:
    11
    Trophy Points:
    0
    A reference is (usually) exactly the same as a pointer, from
    an implementation and performance point of view. They are,
    in that sense, pure syntactic sugar. The only difference is
    that in some cases a reference can be optimized away.
    I'm not saying they are a bad thing, they're good for what
    they are designed for. It's just incorrect to suggest that
    they offer a performance advantage over pointers.

    If you still disagree, consider this C++ and its assembly code
    (from g++ using -S flag):

    Code:
    #include <iostream>
    using namespace std;
    
    void f( int i, int& r, int* p ) {
        ++i;
        ++r;
        ++*p;
    }
    
    int main() {
        int i = 0, r = 1, p = 2;
        cout<< i <<' '<< r <<' '<< p <<endl;
        f( i, r, &p );
        cout<< i <<' '<< r <<' '<< p <<endl;
    }
    I've condensed the assembly for brevity. ## comments are mine.
    Code:
    ...
    __Z1fiRiPi:  ## f's mangled name: Z1(arbitrary) f(our name) iR(int&) iP(int*) i(int)
    pushl	%ebp
    movl	%esp, %ebp        
    incl	8(%ebp)           ## inc value at stack offset 8     (i)
    movl	12(%ebp), %eax    ## load pointer at stack offset 12 (r)
    incl	(%eax)            ## inc indirect (dereference)
    movl	16(%ebp), %eax    ## same as for r                   (p)
    incl	(%eax)
    popl	%ebp
    ret
    ...
    movl	$0, -4(%ebp)      ## Load the variables with 0, 1, and 2.
    movl	$1, -8(%ebp)      ## Note their constant addresses (stack offsets).
    movl	$2, -12(%ebp)     ## i is -4, r is -8, p is -12 (relative to ebp)
    ...
    leal	-12(%ebp), %eax   ## push p's address
    pushl	%eax
    leal	-8(%ebp), %eax    ## push r's address (same as for p)
    pushl	%eax
    pushl	-4(%ebp)          ## push i (value)
    call	__Z1fiRiPi        ## call f
    ...
     
  18. asadullah.ansari

    asadullah.ansari TechCake

    Joined:
    Jan 9, 2008
    Messages:
    356
    Likes Received:
    14
    Trophy Points:
    0
    Occupation:
    Developer
    Location:
    NOIDA
    You all are correct.
    One main Difference is that Extra memory is taken by Pointer( Size depends on machine architecture i.e. 32-bit machine will be 32 bit size of pointer) But in case of reference is Only Alias for that object.

    Some book written that reference is just like a constant pointer. But it's not correct. Just reference 's behabiour is like constant pointer.
     
  19. asadullah.ansari

    asadullah.ansari TechCake

    Joined:
    Jan 9, 2008
    Messages:
    356
    Likes Received:
    14
    Trophy Points:
    0
    Occupation:
    Developer
    Location:
    NOIDA
    One More use of it is to cleanup Code in place of pointer.......
     
  20. Shishir191

    Shishir191 New Member

    Joined:
    Jul 24, 2007
    Messages:
    27
    Likes Received:
    1
    Trophy Points:
    0
    Occupation:
    Software Engineer
    Location:
    Delhi
    Thanks to all.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice