Very quick question about dynamic arrays.

This is a discussion on Very quick question about dynamic arrays. within the C++ Programming forums, part of the General Programming Boards category; I had a very quick question about dynamic multidimensional arrays. If I were to create an array like this code, ...

  1. #1
    Registered User Lawn Gnomusrex's Avatar
    Join Date
    Oct 2008
    Location
    Leading lawn gnomes on the path to world domination! ;o)
    Posts
    13

    Smile Very quick question about dynamic arrays.

    I had a very quick question about dynamic multidimensional arrays.

    If I were to create an array like this code, how would I go about cleaning it up?
    Code:
    int **forest = new int *[10];
    for (int i = 0; i < 10; i++)
    {
      forest[i] = new int[4];
    }
    Would I just call delete it normally, or would I have to do something special?
    Code:
    delete [] forrest;

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    You have to do something special. In setting things up you call new int*[10] followed by a loop where you call new int[4]. In deallocating this memory, you essentially have to work your way backwards - loop while calling delete [] forest[i] followed by a single delete [] forest call after the loop.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User Lawn Gnomusrex's Avatar
    Join Date
    Oct 2008
    Location
    Leading lawn gnomes on the path to world domination! ;o)
    Posts
    13
    Ok, so I would have to do this?
    Code:
    for(int i = 0; i < 10; i++) {
        delete [] forest[i];
    }
    delete [] forest;

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    Yes.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User Lawn Gnomusrex's Avatar
    Join Date
    Oct 2008
    Location
    Leading lawn gnomes on the path to world domination! ;o)
    Posts
    13
    Ok, thank you! I appreciate it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quick question about types...
    By Elysia in forum C++ Programming
    Replies: 32
    Last Post: 12-07-2008, 04:39 AM
  2. Quick question arrays/pointers
    By liljp617 in forum C Programming
    Replies: 6
    Last Post: 10-02-2008, 12:11 PM
  3. dynamic arrays
    By sokermaniac in forum C++ Programming
    Replies: 34
    Last Post: 05-12-2008, 12:41 PM
  4. Question on Arrays
    By shin in forum C Programming
    Replies: 10
    Last Post: 06-06-2004, 01:11 PM
  5. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 05:46 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21