In general, you need to match every new
with a delete
so if, as here, you're juggling a whole array of raw pointers, you will indeed need to delete each in turn.
This can be a serious hassle if the code between new
and delete
isn't straightforward, and almost impossible to ensure if it might throw exceptions. For that reason, you should always use RAII types such as containers and smart pointers to manage dynamic resources. In this case, vector<vector<int>>
might be an appropriate choice.
I guess the real question is "What does
delete
anddelete[]
really do behind the scene?"
If the object type has a non-trivial destructor, it calls that destructor on each object before deallocating the memory. Thus RAII types will automatically release whatever resource they manage through their destructor; but raw pointers don't have destructors, so deleting them will not free any memory that they point to.