Thread: Delete Array of Pointers

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    224

    Unhappy Delete Array of Pointers

    Hi,

    I have an array of pointers:

    Code:
    Bitmap*     g_pTank[3];
    I have a problem cleaning them up...

    Code:
        for (int i = 0; i < 3; ++i)
      {
    	  delete []g_pTank[i];
      }
      
      delete []g_pTank;
    The game crashes when closing down. In particular due to the last line of code:

    Code:
    delete []g_pTank;

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    But g_pTank is not allocated, only the elements in the array are, right?

    It would help if you posted the code that allocates the memory.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Also remember to use delete [] only when you've used new [].
    But since this is C++, you should probably be using smart pointers anyway.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    224
    Thanks guys.

    Here was the code of initialisation - didnt realise I forgot to post it

    Code:
      g_pTank[0] = new Bitmap(hDC, IDB_TANK, g_hInstance);
      g_pTank[1] = new Bitmap(hDC, IDB_TANK2, g_hInstance);
      g_pTank[2] = new Bitmap(hDC, IDB_TANK3, g_hInstance);
    What do you think now?

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You can use smart pointers. If not, then use delete on each element and not on the array itself.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Nov 2006
    Posts
    224

    Question

    Quote Originally Posted by Elysia View Post
    You can use smart pointers. If not, then use delete on each element and not on the array itself.
    smart pointers?

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    In which case, you should use delete (without []) for the three array elements, and not at all for the array itself.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    http://msdn.microsoft.com/en-us/library/bb982026.aspx
    Or if you don't understand, the smart pointer takes ownership of the memory and frees it itself, freeing the burden from the programmer.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. Syntax for constant array of array pointers
    By BMintern in forum C Programming
    Replies: 4
    Last Post: 05-14-2008, 08:21 AM
  3. Array of Pointers to Arrays
    By Biozero in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 02:31 PM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. Creating 2D arrays on heap
    By sundeeptuteja in forum C++ Programming
    Replies: 6
    Last Post: 08-16-2002, 11:44 AM