c++ - Delete a pointer to pointer (as array of arrays) -
i have in code:
double** desc = new double* [size_out]; (int = 0; < size_out; i++) desc[i] = new double [size_in];
how delete desc
?
should do:
delete [] desc;
or
for (int i=0; i<size_out; i++) delete [] desc[i]; delete [] desc;
or
for (int i=0; i<size_out; i++) delete [] desc[i]; delete desc;
?
simple rules follow:
- for each allocation, there has deallocation (ex1 therefore wrong)
- what allocated using
new
should freed usingdelete
, usingnew[]
should deallocated usingdelete[]
, usingmalloc
should deallocated usingfree
(ex3 therefore wrong)
conclusion, ex2 ok.
Comments
Post a Comment