5

so I have a pointer that points to an array of pointers!

    int **matrixPtr;
    matrixPtr = new int*[5];
    for(i=0; i<5; ++i){
        matrixPtr[i]= new int[5];
    }

I'm wondering if this is the proper way to free up the memory!

    for(i=0; i<5; ++i){
        delete [] matrixPtr[i];
    }
    delete [] matrixPtr;

Thanks!

5
  • You are right. How many times you "new", that's how many times you need to delete them Commented Sep 10, 2013 at 5:19
  • 3
    While not an answer to the question, please do consider using std::array or std::vector to make your life easier by avoiding new and delete altogether. Commented Sep 10, 2013 at 5:23
  • It's an assignment for a data structures class so I can't change the implementation...... Commented Sep 10, 2013 at 5:28
  • 1
    Why so afraid of manual allocation? It is not suggested but does not mean you cannot do it! Commented Sep 10, 2013 at 5:31
  • @texasbruce Exception safety, mainly. Commented Sep 10, 2013 at 5:43

2 Answers 2

7

No problem. It's right! You deallocated in the reverse order that you allocated! I don't even think there's another way to do that.

Sign up to request clarification or add additional context in comments.

5 Comments

The best way would be to avoid manual allocation all together.
@ArneMertz and no one further will ever understand how memory handling is done right. I don't think that avoiding manual allocations is "the best way". Understanding allocation procedures is "the best way".
@ArneMertz. Use C++11 or C++14 does not disencourage us to use manual allocation. It depends on the purpose of the developer. You could say "there is another way IF YOU WANT to avoid manual allocation", but it's not the "best way"... it's just "one way"! But I liked your point of view! +1 ;-)
@kronos if you really need to understand manual memory allocation, then yes, you have to try it out. But this should only be advanced level stuff, because you would only need it to implement safe, easy to use components. The problem is that this kind of thing is often done at the beginning of the C++ learning process, when it can do more harm than good.
@WagnerPatriota C++ encourages the use of encapsulation to put the perils of manual resource management into their own classes. C++11/14 just provide handy standard library classes so one does not have to roll their own. However, coping with resource management and something else (e.g. some business logic) violates the SRP.
2

Yes and no. Yes, it's the right way to manually free the memory if you have to allocate it manually like you did.

But no, you should avoid manual allocation and deallocation of memory. If you are stuck with C++03 and without any smart pointers, you should use a vector of vectors. In C++11 you have more options, namely smart pointers and std::array, the latter only if you know the size of the inner or outer dimension or both at compiletime. In C++14 std::dynarray could become an option, too.

2 Comments

yeah my instructor is requiring it be done this way else I would use something more practical!
@Matt Of course, if it's just for the sake of learning memory management, then this is the way to go.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.