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

  • C/C++
  • Thread starter neurocomp2003
  • Start date
  • Tags
    Arrays
In summary, the individual questioned if using "delete [] varname;" would delete multiple arrays in a 3D array structure. The expert advised using a for loop to delete all elements in the array, and warned against relying on the program to automatically deallocate memory. The individual mentioned using "delete [] name" and was advised to correct it. They also mentioned adding scene management technology to handle the large amount of resources used by the array.
  • #1
neurocomp2003
1,366
3
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
  • #2
You need a for loop to delete everything.
 
  • #3
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?
 
  • #4
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.
 
  • #5
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.
 

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

1. What is the syntax for deleting an N-D array in C++?

The syntax for deleting an N-D array in C++ is delete[] arrayName;. This will delete the entire array and free up the memory it was using.

2. Can I delete individual elements of an N-D array in C++?

No, it is not possible to delete individual elements of an N-D array in C++. The delete[] operator can only be used to delete the entire array.

3. What happens if I try to delete an N-D array that has already been deleted?

If you try to delete an N-D array that has already been deleted, it will result in undefined behavior. This can lead to crashes or other unexpected results in your program.

4. Do I need to delete N-D arrays in C++ if I use the new keyword to allocate memory?

Yes, it is important to delete N-D arrays in C++ if you use the new keyword to allocate memory. This is to prevent memory leaks, where the memory allocated for the array is not freed up, leading to a loss of available memory in your program.

5. Is there a difference between deleting an N-D array and using the delete operator on a single pointer?

Yes, there is a difference between deleting an N-D array and using the delete operator on a single pointer. When deleting an N-D array, the delete[] operator will call the destructor for each element in the array. However, when using delete on a single pointer, the destructor for that specific object will be called. It is important to use the correct operator depending on how the memory was allocated.

Similar threads

  • Programming and Computer Science
Replies
31
Views
2K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
25
Views
2K
  • Programming and Computer Science
Replies
4
Views
5K
  • Programming and Computer Science
Replies
22
Views
2K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
2
Views
985
  • Programming and Computer Science
3
Replies
75
Views
4K
  • Programming and Computer Science
3
Replies
89
Views
4K
  • Programming and Computer Science
4
Replies
118
Views
7K
Back
Top