How to pass a double pointer as argument and return it from C++ function

Asked

Viewed 180 times

1

I have a function I need her to be void and that a Double Pole be returned from her by the argument.

void cracking::decompose(char input[][100], int size_S, double* &Atm, int* &ID, int &size_out);
{
vector<double> AtmD;
vector<int> XsD;
...


    Atm = &AtmD[0];
    ID = &XsD[0];
    size_out = size(AtmD);

}

on Main she gets:

int main()
{
    char oi[900][100] = { "1  0.5 C", "2  0.55 N", "3  .5  S" };
    double* xAtm = NULL;
    int* xXs = NULL;
    int tamanho;
    cracking calc;


    calc.decompose(oi, 3, xAtm, xXs, tamanho);



    return 0;
}

although compiling it returns me completely wrong values. Is there any way to do this with pointers?

In fact I realized that the pointers inside my function turn to garbage when I return them to main. Is there any way to keep pointers saved? For example, if you save the address 0x000000000029d680 memory and it corresponds to 23.5, when I leave the function this address no longer corresponds to this value and becomes garbage, there is a way to keep this value?

  • what you tried to do with double* &aponta? Tries double *aponta only. Something else, to return value, your function has to indicate the type of return, and not void. Passing by reference is not return

  • I need it to be as double &value, so rather than being a double argument it is a double pointer so I can input it but also take back the resulting value of it as it works for the int &size

  • "I need" is not justification. Say what you tried to do here, and why it is necessary to point to the address of a variable instead of pointing to the variable. I’m under the impression that you don’t know what you’re doing - in which case, your best help would be a good book of C.

  • I have a vector<double> and I need to point to the first double of this vector, so I can by the pointer of the first element and the number of elements get all this vector back. It is not feasible that I have a function of type vector<double> because in some cases I have more than one vector, struct is also not feasible.

  • We need to see the code snippet where the function is called

  • I made the changes

  • 1

    Atm and id return garbage, because the Atmd and Xsd vectors are destroyed in the function return. Do you really need the address of these vectors? Isn’t the value of the first element enough? Is this function signature your creation or did it pass to you? I don’t usually find a reference for a pointer as a function parameter.

  • Marcos, I need all the elements of the vector but I can’t pass the vector either as a return or as an argument because I’m going to exterminate this function in C. Anything that works to get the whole vector would be of great help.

  • 1

    In this case, I think you better replace the vectors with arrays of C. And you will have to define who will release this memory later.

Show 4 more comments

1 answer

0

The point is that both vector<double> AtmD; and vector<int> XsD; are allocated to the stack when your method decompose is called. Once this method returns, the context of the function is destroyed as well as the two allocated vectors. So these addresses point to garbage.

If you really need to do this, you can allocate an array static (as in C) so that the array is not destroyed in the output, however this solution is not thread-safe. You can also allocate the vectors in the main and move into their function.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.