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

  • Context: C/C++ 
  • Thread starter Thread starter neurocomp2003
  • Start date Start date
  • Tags Tags
    Arrays
Click For Summary

Discussion Overview

The discussion revolves around the proper use of the "delete []" operator in C++ for deallocating memory allocated for a 3-D array. Participants explore whether a single "delete [] varname;" is sufficient to delete the entire array structure or if a more detailed approach is necessary.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant questions if "delete [] varname;" can delete a 3-D array created with multiple levels of dynamic allocation.
  • Another participant suggests that a for loop is necessary to delete each level of the array before using "delete [] name;".
  • A follow-up post outlines a proposed deletion strategy using nested for loops to delete each sub-array, but contains a typographical error in the code.
  • Participants discuss the implications of failing to deallocate memory, with one noting that while the operating system may free memory upon program termination, it is considered poor practice to rely on this behavior.

Areas of Agreement / Disagreement

There is no consensus on whether "delete [] varname;" alone is sufficient for 3-D arrays, as participants present differing views on the necessity of additional deletion steps.

Contextual Notes

Participants express uncertainty regarding the correct syntax for deletion and the implications of memory management practices in C++. There are also unresolved questions about the behavior of memory allocation and deallocation upon program termination.

Who May Find This Useful

Readers interested in C++ memory management, dynamic array allocation, and programming best practices may find this discussion relevant.

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.
 

Similar threads

  • · Replies 31 ·
2
Replies
31
Views
3K
  • · Replies 25 ·
Replies
25
Views
3K
  • · Replies 22 ·
Replies
22
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 4 ·
Replies
4
Views
7K
Replies
89
Views
7K
  • · Replies 5 ·
Replies
5
Views
2K
Replies
47
Views
5K
  • · Replies 2 ·
Replies
2
Views
2K