Antonio1996
Antonio1996

Reputation: 756

How to return dynamically allocated array from function and correctly delete it

I'm new to C++ and I have a problem with memory management.

I have one function a() that calls 3 functions (b(), c(), d()), each one of which returns a dynamically allocated array of MyClass objects:

void a(){
    MyClass * one=b();
    MyClass * two=c();
    MyClass * three=d();
    //operate with 3 array (one, two and three)
    delete [] one;
    delete [] two;
    delete [] three;
}

MyClass * b(){
    MyClass * array=new MyClass[2000];
    //many operations on array
    return array;
}

MyClass * c(){
    MyClass * array=new MyClass[2000];
    //many operations on array
    return array;
}

MyClass * d(){
    MyClass * array=new MyClass[2000];
    //many operations on array
    return array;
}

After many operations in a() I must delete the 3 arrays that I created with the 3 functions. If I do it using the 3 delete [] expressions like those in the code above, is it ok?

I asked myself this question because I think that this code deallocates everything correctly, but analyzing the memory allocation of my c++ program I see no evidence of this deletion.

Upvotes: 2

Views: 2171

Answers (2)

Yes, what you're doing is correct.

However, note that manual memory management is prone to bugs, be it memory leaks or double deletion. It's much better to forget about naked new and especially delete, and instead use RAII classes for handling this safely.

In your case, you should replace the pointers to dynamically-allocated arrays with std::vector. std::vector is the way to spell "dynamically allocated array" in C++. Once you do that, you will know that the vector will take care of deallocating the memory correctly, even in the presence of exceptions.

This is what your code could look like with std::vector:

void a(){
    auto one=b();
    auto two=c();
    auto three=d();
    //operate with 3 array (one, two and three)

    //nothing to do here, the vectors will deallocate memory correctly when they go out of scope
}

std::vector<MyClass> b(){
    std::vector<MyClass> array(2000);
    //many operations on array
    return array;
}

std::vector<MyClass> c(){
    std::vector<MyClass> array(2000);
    //many operations on array
    return array;
}

std::vector<MyClass> d(){
    std::vector<MyClass> array(2000);
    //many operations on array
    return array;
}

As for why you can't see effects of the deallocation: memory freed by delete need not be immediately returned to the operating system. Quite often, the C++ runtime will keep hold of it and use it to satisfy future allocation requirements of your program.

Upvotes: 3

r3mus n0x
r3mus n0x

Reputation: 6144

For reliable memory-management you should use smart pointers such as std::unique_ptr. So when your function is returning a dynamically allocated value it should instead return std::unique_ptr to that value to make sure that this value will be deallocated.

std::unique_ptr<MyClass[]> array(new MyClass[2000]);

In your case however you should consider using std::vector instead of raw array.

Upvotes: 5

Related Questions