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.
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.

Similar threads

Back
Top