C/C++ Does 'delete [] varname;' delete multiple arrays in C++?

  • Thread starter Thread starter neurocomp2003
  • Start date Start date
  • Tags Tags
    Arrays
AI Thread Summary
The discussion centers on memory management in C++ when dealing with dynamically allocated multi-dimensional arrays. It clarifies that using "delete [] varname;" is insufficient for a 3D array; a proper deallocation requires nested loops to delete each sub-array before deleting the main array. Specifically, the correct sequence involves deleting each element in the innermost array first, then the second dimension, and finally the outermost array. Additionally, it addresses the consequences of failing to deallocate memory, emphasizing that while the operating system may reclaim memory upon program termination, relying on this behavior is poor practice and can lead to inefficient memory usage during program execution. The conversation also touches on the importance of correctly spelling the delete command to avoid errors.
neurocomp2003
Messages
1,359
Reaction score
4
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= new type*[n];
for i,j: name[j]=new type[n];
and name[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[j] then delete name and then name.
 
Technology news on Phys.org
You need a for loop to delete everything.
 
coo, thanks so its

for i,j: delte name[j]
for i: delete name
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?
 
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.
 
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 got to add in some scene mgmt tech.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

Back
Top