This is how i allocate dynamic memory for a 2D array
char **twod;
twod=new char*[count];
for (int i = 0; i < count; i++)
{
twod [i] = new char [MAX];
}
This is how i release the memory for a 2D array
for (int i=0; i<count;i++)
{
delete [] twod [i];
}
delete [] twod;
How do i know i have successfully released everything and there is no memory leak???
Run the code in valgrind or any such memory leak detection tool.
If you want you could also overload the
newanddeleteoperators for your class and do the bookeeping yourself but that it too much effort so you are much better off setting with a memory leak detection tool.Ofcourse I consider the example only an sample example and not the code one will usually go for because: