0

According to the answer https://stackoverflow.com/a/11842442/5835947, if you code like this, function parameter Bubble * targetBubble will be copied inside the function.

bool clickOnBubble(sf::Vector2i & mousePos, std::vector<Bubble *> bubbles, Bubble * targetBubble) {
    targetBubble = bubbles[i];
}

However, I made a test, finding that a pointer as function parameter will be the same as the outside one, until I changed it's value:

// c++ test ConsoleApplication2.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "c++ test ConsoleApplication2.h"
using namespace std;
#include<iostream>




int main()
{
    int a= 1;
    int* pointerOfA = &a;

    cout << "address of pointer is" << pointerOfA << endl;
    cout << *pointerOfA << endl;
    func(pointerOfA);
    cout << *pointerOfA << endl;





}


void func(int *pointer)
{
    cout << "address of pointer is " << pointer  <<" it's the same as the pointer outside!"<<endl;

    int b = 2;
    pointer = &b;
    cout << "address of pointer is" << pointer <<" it's changed!"<< endl;

    cout << *pointer<<endl;


}

Output is below:

address of pointer is0093FEB4
1
address of pointer is 0093FEB4 it's the same as the pointer outside!
address of pointer is0093FDC4 it's changed!
2
1

So, the truth is that, a pointer as function parameter will not be copied, until it's changed, right? Or am I missing something here?

guo
  • 9,674
  • 9
  • 41
  • 79
  • 2
    You're saying "*address of pointer is*" but you're printing the value of the pointer. And yes, C++ has "pass by value" semantics, so the pointer and all function parameters are copied, unless they are references. – juanchopanza Jun 04 '17 at 12:16
  • When you mail a package, you put an address label on it. The package is then moved around until it reaches the destination. The location of the physical piece of paper which is the address label is changing all the time, but the information on it remains constant. – John Zwinck Jun 04 '17 at 12:20
  • I'm voting to close this question as off-topic because it's based on a simple misconception what conforms a _pointer value_ and the _value held at a pointer's_ address. – πάντα ῥεῖ Jun 04 '17 at 12:59

2 Answers2

5

The pointer (variable holding just an address - usually just a 32 or 64 bit integer) is copied. What the pointer points to is not.

So yes, you are missing something. You need to understand that a pointer is not the object it points to. It's just a small integer value saying "that's the object over there" - and copying that pointer is cheap and doesn't change what it's pointing at.

Jesper Juhl
  • 30,449
  • 3
  • 47
  • 70
0

Pointers are used because instead of copying entire object which may be costly it will copy/pass to function as arguments the address of the object. When you are passing the pointer to the function and making changes to it outside the function or in the function it will modify the same object. You are printing the value of the pointing object by doing *p. If you want to check if pointer variable is copied then output p and check which may be different.

Jesper Juhl
  • 30,449
  • 3
  • 47
  • 70
rakesh
  • 4,368
  • 1
  • 19
  • 13