简体   繁体   中英

Deallocating all memory associated with a struct in C++

I have a struct, which contains a few vectors, defined as follows:

#include <vector>
using namespace std;

struct data{
    vector<float> x;
    vector<float> y;
    vector<float> z;
}

Which I later use as such:

data d;
for(int i; i<3; i++){
    d.x.push_back(i)
    d.y.push_back(i*2)
    d.z.push_back(i*3)
}

And now I want to safely delete data in a way the completely deallocates all of the memory associated with it. I think that the way to do this is to write a simple destructor which clears and deallocates each field of data , and then delete the object:

struct data{
    vector<float> x;
    vector<float> y;
    vector<float> z;

    ~data(){
        vector<tempObject>().swap(x);
        vector<tempObject>().swap(y);
        vector<tempObject>().swap(z);
    }
}

and then this should work:

data d;
    for(int i; i<3; i++){
        d.x.push_back(i)
        d.y.push_back(i*2)
        d.z.push_back(i*3)
    }
delete data;

where I've made use to the top answer here to deallocate the vectors.

Will this work? If not, can you describe why and/or suggest an alternative? I understand that the memory will be freed once d leaves scope, but I need to free the memory before that happens.

When the struct is destroyed its members will be destroyed (in reverse order of declaration). The vector s being thus destroyed will clean up their own memory.

No need for a custom destructor.

And now I want to safely delete data in a way the completely deallocates all of the memory associated with it.

In C++ there are variables with automatic, static and dynamic storage duration. Lifetime of automatic storage duration that you have in your example and static one is controlled by compiler, so you cannot change their lifetime (small exception is prolongation of rvalue by reference, but that is unrelated here). If you need to manually control lifetime you need to create a variable with dynamic storage duration:

auto d = std::make_unique<data>();
d->x.push_back(1.0);
...
d.reset(); // terminate of lifetime of variable pointed by d manually

no need for delete, since you have "data d" as stack object.

vector (as most other stl containers take care of cleanup for you).

you also do not need a custom destructor for the struct

if you use modern c++ and manage your heap objects on shared_ptr or unique_ptr, you (almost) never need to use the keywords new and delete.

example:

#include <vector>
#include <memory>

struct MyData
{
    std::vector<float> x;
    std::vector<float> y;
    std::vector<float> z;
};

int main()
{
    //example 1: stack object
    {
        MyData d;
        for(int i = 0; i<3; i++)
        {
            d.x.push_back(i);
            d.y.push_back(i*2);
            d.z.push_back(i*3);
        }
    } // scope of d ends, everything including the vectors are cleaned up


    //example 2: heap object  
    {
        std::unique_ptr<MyData> dataPtr = std::make_unique<MyData>();
        for(int i = 0; i<3; i++)
        {
            dataPtr->x.push_back(i);
            dataPtr->y.push_back(i*2);
            dataPtr->z.push_back(i*3);
        }
    } // scope of dataPtr ends, everything will be cleaned up

    //example 3: heap object, clean up within scope
    {
        std::unique_ptr<MyData> dataPtr = std::make_unique<MyData>();
        for(int i = 0; i<3; i++)
        {
            dataPtr->x.push_back(i);
            dataPtr->y.push_back(i*2);
            dataPtr->z.push_back(i*3);
        }

        dataPtr.reset(); // this will clean-up whatever the pointer points to

    } // scope of dataPtr ends, ptr itself is destoryed

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM