Saving data from a class that goes beyond?

I work with the "foo" class from an external library that computes and stores large amounts of data. After the calculation is complete, I want to save only the result data array, which is about half the size of "foo". Foo provides a pointer to the result data using a function RawResultsArray()with a type PNSt3__17complexIdEE. So far I have done this:

int N; //length of the results array
complex<double> * results_to_keep;

{
    foo myFoo;

    myFoo.findResults();

    results_to_keep = new complex<double>[N];
    for (auto i=0; i<N; i++)
    {
        results_to_keep[i] = myFoo.RawResultsArray()[i];
    }
}
//work with results_to_keep in further code...
delete [] results_to_keep;
//do other memory intensive stuff in further code...

However, I work with limited memory and cannot afford to have an array results_to_keepand a class myFoo. Is there a way to save the length data N*sizeof(complex<double>)in myFoo.RawResultsArray()after leaving the scope in line 14 without temporarily allocating the full array again?

I played unsuccessfully with smart pointers. I think my best attempt is:

std::unique_ptr<complex<double>[]> results_to_keep;
{
    foo myFoo;
    myFoo.findResults();

    std::unique_ptr<complex<double>[]> temp (move(myFoo.RawResultsArray());
    results_to_keep = move(temp);
}
//work with results_to_keep in further code...
results_to_keep.reset();
//do other memory intensive stuff in further code...

, results_to_keep , . malloc: *** error for object 0x106000010: pointer being freed was not allocated.

: typeid(myFoo.RawResultsArray()).name() PNSt3__17complexIdEE. Apple LLVM 8.1.

+4
1

, .

foo "" , , "" , .

+4

Source: https://habr.com/ru/post/1676481/


All Articles