1

first I am going to try explain what I want to do and then explain the behavior that I am experimenting

I have a main.cpp file and I have some objects, for example, x, y, z. I want to define this objects in main.cpp and pass them to a function to initialize them, and finally use them.

this is part of my code

int main(){
    X* x = NULL;
    Y* y = NULL;
    Z* z = NULL;

    function(x,y,z);

    x->getX();

    return 0;
}

void function (X* x, Y* y, Z* z){
    x = new X();
    y = new Y();
    z = new Z();
}

This code results in a segmentation fault in x->getX();. I run the program with gdb and the objects was created correctly in function() but when the execution exits from the function the objects was destroyed and x = y = z = 0x0.

I supposed that when you pass an object by reference to function you can modify the object's member but not the objects itself.

So, my question is how can declare a object in main, then pass it to a function to initialize and finally use it in main.

Thanks.

RdlP
  • 1,366
  • 5
  • 25
  • 45
  • 4
    [Pass by reference.](http://www.tutorialspoint.com/cplusplus/passing_parameters_by_references.htm) You are passing by value, this makes a copy of your types. – Fantastic Mr Fox Dec 28 '15 at 23:11
  • You don't want to modify the pointer itself, you just want to change what it points to. So passing the pointer by reference (instead of by value, as your code does) is the perfect solution. – David Schwartz Dec 28 '15 at 23:15
  • The code in `main` doesn't create any objects. It creates **pointers** to objects, and sets the pointers to NULL. – Pete Becker Dec 28 '15 at 23:24

3 Answers3

3

You need to pass by reference. Currently you are passing by value which will create a copy of your value. Here is a simple example, consider 2 functions:

void dontEdit(int a) {
   a = 2;
}

void edit(int &a) {
   a = 2;
}

In dontEdit a will be copied and changing the value of a will have no effect on the outside world. In edit you are giving the function a reference to the memory that holds a so you are able to detect this change out of the scope of the function. Consider their use here:

int main() {
    int a = 5;
    cout << "a is: " << a << endl;
    dontEdit(a); // this is pass by value, a copy of a is made.
    cout << "a is: " << a << endl;
    edit(a); // this is pass by reference, a is edited directly. 
    cout << "a is: " << a << endl;
    return 0;
}

The output will be:

a is: 5
a is: 5
a is: 2

Live example

In your question you have the same problem. You want to edit the main scope x, but you pass it by value. You will create a copy of the pointer x and assign it to new X(). Then when you return, that copy of x no longer exists and the memory you have created is left hanging, you have no way to find it. So what you need to do it pass by reference:

void function (X* &x, Y* &y, Z* &z) {

As a final note, c++ has no automated garbage collection, so for every new there should be a delete, otherwise you run the risk of memory leaks.

Fantastic Mr Fox
  • 32,495
  • 27
  • 95
  • 175
  • Yes, I knew all that you have explained in your post. My problem was that I thought that objects passes as reference by default. Your post is very complete about the topic, thanks – RdlP Dec 28 '15 at 23:49
1

Your function should be declared as

void function (X* &x, Y* &y, Z* &z)

because you want to pass the parameters as reference to be able to modify the pointer x, y and z.

X* means pointer to X

& means reference of ...

therefore

X* &x means a reference of a pointer to X

Have a look at the following question for further details, it contains the basics you need to know:

How to pass objects to functions in C++?

Community
  • 1
  • 1
ViRuSTriNiTy
  • 5,017
  • 2
  • 32
  • 58
0

I think you should use, IMHO the really good property, pass-by-reference. But, If you want to use, indirect pass-by-reference, actual parameters should in callee precede with & as function(&x,&y,&z);. But it is not good for C++. Because it has real pass-by-reference power. Am I wrong?