0

I want to have a explanation with respect to variable created on stack memory, what is the difference between pointer and reference. I understand in the case of calling by pointer, for example, the following things are happening.

#include <iostream>
using namespace std;

void swap(int *x, int *y){
    int temp;
    temp  = *x;
    *x = *y;
    *y  = temp;    
   }

int main(){
    int i1 = 3, i2 = 7;
    swap (&i1,&i2);
    return 0;
 }
  • Two variables i1, i2 are created on stack framed to the main function.
  • at calling 'swap' function, two more variables x, y, are created on stack, framed to the 'swap function. These two variables are initialized at the address of i1 and i2.
  • a temp variable is created on stack framed to 'swap' function. All of x,y,temp disappear after 'swap'.

Could someone explain in a similar way what happens when I use 'call by reference'? In particular, is there a variable x and y created on stack when calling 'swap'? If so, what are they?

#include <iostream>
using namespace std;

void swap(int &x, int &y){
    int temp;
    temp  = x;
    x = y;
    y  = temp;    
}

int main(){
    int i1 = 3, i2 = 7;
    swap (i1,i2);
    return 0;
}

Thank you!

  • 3
    This should be covered by any good C++ book. What does your say about references? – Stephen Newell Aug 17 '22 at 01:12
  • references can't be null and you get to use them like the original object instead of having to user pointer access. – NathanOliver Aug 17 '22 at 01:15
  • @StephenNewell Hi, I have not used a proper C++ book but I have read many different course materials and online references. There was no comment on how variables are created on stacks when using them in function calls. I however do understand "assign by reference" is assigning an alias to the original variable, just don't know what the implication is with regard to memory management. –  Aug 17 '22 at 01:21
  • 3
    Godbolt to the rescue. The compiler generated code is **identical** for references as it is for pointers. https://www.godbolt.org/z/66333Ps63 – selbie Aug 17 '22 at 01:25
  • @harold That is where I am very confused... I think I need to look at "assigning by reference" and "calling by reference" differently? Because if I do {int i = 3; int &j = i;}, and if I print out memory address cout<<&i, cout<<&j, they are exactly the same, meaning there isn't a pointer variable being created at all. –  Aug 17 '22 at 01:30
  • @selbie Thank you for this. I have zero knowledge about assembly right now.. but good to know "calling by reference" and "calling by pointer" are the same in this case. –  Aug 17 '22 at 01:32
  • 1
    Consider a reference a synonymous *alias* ; a pointer a *roadmap*. A reference means anything you do with that reference is synonymous with doing said-same activity with the *referenced* object. Applying `&` to both yields the same address, for example. Pointers are different. A pointer *shows* you where the pointed-to object resides (the pointer value), and you *dereference* said-same to reach it (the object). Also, it can be altered (change pointer values) to some other object, or even no object (null), *neither* of which are possible with the synonymous alias mechanics of a reference. – WhozCraig Aug 17 '22 at 01:35
  • @WhozCraig Hi, thank you very much for the detailed explanation. That is my understanding of the difference between the two, too. That makes me very surprising to find out, as "selbie" pointed out above, that the calling by reference and calling by pointer has the same generated code... –  Aug 17 '22 at 01:41
  • 1
    A reference is typically implemented as a pointer that is always dereferenced. So `int& j = i` is internally `int* pj = &i` and then `&j` becomes `&*pj` which simplified to `pj` which equals `&i`. – Raymond Chen Aug 17 '22 at 01:50
  • @RaymondChen WOW this explains everything... Nobody had put it in this way. Thank you so much! –  Aug 17 '22 at 01:53
  • And a big bonus, a reference cannot be null. So when you use a reference you don't have to do nullptr checks. When you want to pass an object (class instance) to a function you can pass a (const) reference to that object. And then the object will not be copied, and you wil be able to use the reference as if it was the object. – Pepijn Kramer Aug 17 '22 at 03:05

1 Answers1

0

you can use both pointers and reference interchangeably if you are working on the value attribute. for example i=4 or *i=4. The advantage of using pointers is that you can manipulate the address itself. Sometimes you will need to modify the address (point it to something else ...) this is when pointers are different for reference; pointers allow you to work directly on the address, reference won't