delete [] *M;
is the same as delete [] M[0]
, so it is not equivalent to deleting all M[i]
in a loop because only the first one would be deleted. The loop is the correct way to avoid memory leaks.
Or better, use std::vector
instead of manual allocations and you won't need to worry about deleting pointers.