Yes and no. Yes , this is the right way to manually free memory if you need to allocate it manually, just like you do.
But no , you should avoid manually allocating and freeing 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 internal or external dimension, or both in compiletime. In C ++ 14, std::dynarray
can also become an option.
source share