-3

I'm practicing C++, and facing the Pass-by-value/reference topic. What I learned from BroCode is that, when we call a swap function by the parameters' value, both of their memory_addresses will be different from the original ones.

#include <iostream>

using std::cout;
using std::endl;

void swap_byValue(int dog, int cat);
void swap_byReference(int &dog, int &cat);
void print(int dog, int cat);

int main(){
    int dog = 100;
    int cat = 999;

    cout << "*****************************" << endl
         << "*  variable_name  *  value  *" << "      (memory_address)" << endl;

    print(dog, cat);

    swap_byValue(dog, cat);
    print(dog, cat);
    return 0;
}

void swap_byValue(int dog, int cat){
    int temp;
    temp = dog;
    dog = cat;
    cat = temp;
}
void swap_byReference(int &dog, int &cat){
    int temp;
    temp = dog;
    dog = cat;
    cat = temp;
}

void print(int dog, int cat){
    cout << "*****************************" << "----->  " << &dog << endl
         << "*       dog       *   " << dog << "   *" <<endl
         << "*****************************" << "----->  " << &cat << endl
         << "*       cat       *   " << cat << "   *" <<endl; 
}

The result after running is down below:

*****************************
*  variable_name  *  value  *      (memory_address)
*****************************----->  0x42295ffc30
*       dog       *   100   *
*****************************----->  0x42295ffc38
*       cat       *   999   *
*****************************----->  0x42295ffc30
*       dog       *   100   *
*****************************----->  0x42295ffc38
*       cat       *   999   *

But shouldn't the memory-address of variable dog(before swapping) differ from variable dog(after swapping)? They're both 0x42295ffc30 right now. Is there anything I misunderstood or did wrong? Thanks a lot.

ian
  • 55
  • 4
  • 5
    `void swap_byValue(int dog, int cat)` simply hasn't any effect, and is even likely to be optimized away by the compiler. `print()` outputs arbitrary stack addresses. – πάντα ῥεῖ Jul 13 '23 at 19:40
  • 2
    you can't change the address a reference points to nor can you change the address of a local variable – Alan Birtles Jul 13 '23 at 19:42
  • 1
    what is BroCode? What you learned was wrong – 463035818_is_not_an_ai Jul 13 '23 at 19:44
  • 1
    What happens when the compiler decides to pass values in registers, since you are passing by value (copy)? Registers don't have memory addresses! – Thomas Matthews Jul 13 '23 at 19:45
  • 2
    The address of `dog` *inside* `swap_byValue` will be different than the one in `main`, because it will be copied. The address of `dog` in `swap_byReference` will be the same as address of `dog` in `main`, because reference is basically an alias for a variable. – Yksisarvinen Jul 13 '23 at 19:48
  • From the BroCode youtube page: *Coding bootcamps hate him! See how he can teach you to code with this one weird trick...* The one weird trick is Content Farming for fun and profit! – user4581301 Jul 13 '23 at 19:58
  • The code I wrote is not from the youtube channel, I was just practicing the concept and trying to make myself clear at the points of memory. – ian Jul 13 '23 at 20:02
  • 3
    Regardless, [get a good book](https://stackoverflow.com/q/388242/4581301). You cannot learn C++ from a 6 hour video. There is simply too much to learn. It takes months to be able to learn to use C++ effectively and years to become good at it. – user4581301 Jul 13 '23 at 20:05
  • 2
    I think there was no doubt that you wrote the code, based on the wrong concept you picked up from the video. Don't get fooled by random guys trying to teach you a language. Use books and/or proper courses – 463035818_is_not_an_ai Jul 13 '23 at 20:05
  • @ian possible duplicate: [What's the difference between passing by reference vs. passing by value?](https://stackoverflow.com/questions/373419/whats-the-difference-between-passing-by-reference-vs-passing-by-value) – πάντα ῥεῖ Jul 13 '23 at 20:18
  • 1
    good introductory videos do exists afaik but I am not aware of any I can recommend to a beginner. Conference talks are usually high quality, but you need the basics from elsewhere – 463035818_is_not_an_ai Jul 13 '23 at 20:36

2 Answers2

3

I wouldn't think so much in terms of addresses.

When you pass a parameter by value, the function gets its own copy of whatever you passed. So when you do the swap, it just swaps its own copies of the to values.

When you pass by reference, you get something that's basically just an alias for the original value. So when you swap those, the original values get swapped.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
1

The results you get are to be expected. It is not possible to change the address of an object. What can be done for example is to copy an object, then you have a new object at a different address and the old object at the same address. A reference on the other hand does not create a new object it merely refers to the original object, hence has the same address.

Calling void swap_byValue(int dog, int cat) has no effect whatsoever and void swap_byReference(int &dog, int &cat) swaps the values of the parameters. What you should actually be using is std::swap to swap values of two variables.

The addresses you print in print are those of the function local variables dog and cat. You can avoid confusion by using distinct names:

void print(int a, int b){
    cout << "*****************************" << "----->  " << &a << "\n"
         << "*       a         *   " << a << "   *\n"
         << "*****************************" << "----->  " << &b << "\n"
         << "*       b       *   " << b << "   *\n"; 
}

This prints the addresses of the two variables a and b which are copies of the arguments passed to the function. When you call the function twice or more they may happen to end up in the same place in memory. This is possible because after the function returns they are gone.

If you print the addresses of dog and cat in main you will also see that they do not change after swapping.

I can only guess what you misunderstood, perhaps you wanted to see this:

#include <iostream>

void by_value(int x) { std::cout << "by_value: " << &x << "\n"; }
void by_ref(int& y) { std::cout << "by_ref: " << &y << "\n"; }

int main() {
    int z = 42;
    std::cout << "in main: " << &z << "\n";
    by_value(z);
    by_ref(z);
}

Possible output:

in main: 0x7ffca8f5989c
by_value: 0x7ffca8f5987c
by_ref: 0x7ffca8f5989c

z in main and y in by_ref do have the same address, because its the same object. y is a reference to z. The address of the reference is that of the original object. x in by_value has a different address because it is a copy.

What I learned from BroCode is that, when we call a swap function by the parameters' value, both of their memory_addresses will be different from the original ones.

That is not correct.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • Sir thank you so much! I now know the problem is about the print() function, cause if I call the print(), after all it still calls the local variables in main()! Thank you so much! – ian Jul 15 '23 at 12:38