Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/articles/cpp-tutorials/)
-   -   Difference between Pointers and Reference in C++ (http://www.go4expert.com/articles/difference-pointers-reference-cpp-t5492/)

pritish 11Sep2007 11:50

Re: Difference between Pointers and Reference in C++
 
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.

Kailash 12Sep2007 12:46

Re: Difference between Pointers and Reference in C++
 
very appropriately describe the difference between pointer and reference. I find it very valuable

sashimi 23Nov2007 14:17

Re: Difference between Pointers and Reference in C++
 
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.

sreeramu 23Nov2007 17:21

Re: Difference between Pointers and Reference in C++
 
Thank you good difference...

sreeja 10Dec2007 15:05

Re: Difference between Pointers and Reference in C++
 
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.

Shishir191 7Jan2008 19:08

Re: Difference between Pointers and Reference in C++
 
Quote:

Originally Posted by sreeja
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.

Good

oogabooga 9Jan2008 05:03

Re: Difference between Pointers and Reference in C++
 
Quote:

Originally Posted by DaWei
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.

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


asadullah.ansari 11Jan2008 10:17

Re: Difference between Pointers and Reference in C++
 
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.

asadullah.ansari 11Jan2008 10:18

Re: Difference between Pointers and Reference in C++
 
One More use of it is to cleanup Code in place of pointer.......

Shishir191 25Feb2008 15:11

Re: Difference between Pointers and Reference in C++
 
Quote:

Originally Posted by asadullah.ansari
One More use of it is to cleanup Code in place of pointer.......

Thanks to all.


All times are GMT +5.5. The time now is 01:35.