Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C++: Deleting N-D arrays

  1. Dec 17, 2005 #1
    Was wondering if the "delete [] varname;" deletes multiple arrays


    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.
  2. jcsd
  3. Dec 17, 2005 #2


    User Avatar
    Staff Emeritus

    You need a for loop to delete everything.
  4. Dec 17, 2005 #3
    coo, thx 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?
  5. Dec 17, 2005 #4


    User Avatar
    Staff Emeritus

    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.
  6. Dec 17, 2005 #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 gotta add in some scene mgmt tech.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook