I hate stackoverflow
I hate stackoverflow

Reputation: 65

Difference between pointer and pass by refenrence

I'm little bit confused with pointer and pass by reference.

This is my code.

int y = 60;
int *x = &y;

int foo(int &something){
    something = 6;
    return 0;
}

foo(y);

this is my question, if foo takes address of the pointer, shouldn't I put foo(x) instead of foo(y)?

isn't x address of the y? if so, why can't I put foo(x)?

Upvotes: 1

Views: 80

Answers (4)

Ahmad Siavosh
Ahmad Siavosh

Reputation: 676

Take a look at the following C++ code

void foo(int *x){
    *x = 1;
}
void bar(int &x){
    x = 1;
}
int main(){
    int y = 2;
    foo(&y);
    bar(y);
}

And this is the assembly code generated by g++ and command-line argument -S

    .file   "main.c"
    .text
    .globl  __Z3fooPi
    .def    __Z3fooPi;  .scl    2;  .type   32; .endef
__Z3fooPi:
    pushl   %ebp
    movl    %esp, %ebp
    movl    8(%ebp), %eax
    movl    $1, (%eax)
    popl    %ebp
    ret
    .globl  __Z3barRi
    .def    __Z3barRi;  .scl    2;  .type   32; .endef
__Z3barRi:
    pushl   %ebp
    movl    %esp, %ebp
    movl    8(%ebp), %eax
    movl    $1, (%eax)
    popl    %ebp
    ret
    .def    ___main;    .scl    2;  .type   32; .endef
    .globl  _main
    .def    _main;  .scl    2;  .type   32; .endef
_main:
    pushl   %ebp
    movl    %esp, %ebp
    andl    $-16, %esp
    subl    $32, %esp
    call    ___main
    movl    $2, 28(%esp)
    leal    28(%esp), %eax
    movl    %eax, (%esp)
    call    __Z3fooPi
    leal    28(%esp), %eax
    movl    %eax, (%esp)
    call    __Z3barRi
    movl    $0, %eax
    leave
    ret

Indeed the concept of pass by reference has its own philosophy which has been mentioned repeatedly, but as you can see in the implementation phase (the phase which matters) they are the same (the same assembly code has been generated for both pointer and reference as you can see). I've seen people encountered problems when using 'pass by reference' because it has not been implemented the way one may expect; So perhaps it'd be better as they are the same in the implementation and they cause confusion, you go with pointer which has its roots in lower levels. Hope it helps :-)

Upvotes: 1

Ana M
Ana M

Reputation: 467

foo is taking a reference to an integer - not a pointer. A reference is very similar to a pointer since behind the scenes what gets passed is the address of the integer that you pass to the function. In your call, the address (on the stack) of the y variable is passed.

Since a pointer is also an address, they are similar. With a pointer, the way you would access the variables would differ - with pointers you need to be more explicit.

The code below does the same as your code, but uses different syntax.

int foo_pointer(int *pointerToSomething)
{
    *pointerToSomething = 6; // The '*' de-references the pointer, so you are assigning what goes at the address referred to
    return 0;
}
foo_pointer(&y); //to pass the address of you explicitly

Typically pointers are used to refer to dynamically (heap) allocated structures or objects - not for a case such as your example.

Upvotes: 0

shibley
shibley

Reputation: 1668

If you were passing by pointer, than you could use foo(x). However, a pass by reference is not equivalent to a pass by pointer - although the syntax may mistakenly lead you to believe that they are compatible. Your function is passing something as a reference. If something is an int, than an int variable should be supplied - something would not be compatible with a pointer to an int.

You can find a more complete description of these mechanisms here.

Upvotes: 1

dkamins
dkamins

Reputation: 21918

Function foo does not take "the address of the pointer" (not sure what you even mean by that really... technically you could say that foo(int **something) takes the address of a pointer).

The function as you have it takes an int reference. Don't get hung up on the fact that the & symbol is used in both cases. It's similar but different, and the required syntax differs accordingly.

Upvotes: 2

Related Questions