PDA

View Full Version : C++: Deleting N-D arrays


neurocomp2003
Dec17-05, 01:21 AM
Was wondering if the "delete [] varname;" deletes multiple arrays

eg...

I coded a 3-D array as follows:

type ***name;
name = new type**[n];
for i: name[i]= new type*[n];
for i,j: name[i][j]=new type[n];
and name[i][j][k] is and element in the array

now does
"delete [] varname;" suffice to delete the 3D array?
or do i have to first delete name[i][j] then delete name [i] and then name.

dduardo
Dec17-05, 01:27 AM
You need a for loop to delete everything.

neurocomp2003
Dec17-05, 04:23 AM
coo, thx so its

for i,j: delte name[i][j]
for i: delete name[i]
delete [] name?

oh yeah one other Q, if i allocate memory and forget to deallocate...when the programm shuts down does it autodeallocate for me? or do i have to turn off my system?

dduardo
Dec17-05, 08:37 AM
Yes, that works, just spell delete correctly.

Yes it will free the memory, but don't rely on it. That is bad programming because while you run the program you'll be wasting a lot of memory.

neurocomp2003
Dec17-05, 02:38 PM
thanks dduardo...heh i was just using "delete [] name" now i should go correct it. Its fun to watch 1000 3D spherical (billiard) balls colliding eating up the resources, yay for a double for loop...now i gotta add in some scene mgmt tech.