Deleting an array from the heap

This is a discussion on Deleting an array from the heap within the C++ Programming forums, part of the General Programming Boards category; I have never been in a situation where I wanted to use one until now, so I was a little ...

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    27

    Deleting an array from the heap

    I have never been in a situation where I wanted to use one until now, so I was a little surprised when I went to delete it and got a "Debug Assertion Error". I can access the array directly through the pointer as though the pointer itself were the array, but I cannot delete the pointer without that error popping up.

    I don't want the memory to be sitting on the stack when I am done using it, so I would rather not declare the array normally if it can be avoided. I thought about using a linked list (the array items are class objects), however when I finish debugging the class I am going to implement a linked list into its container class anyway. This array is needed only for a short portion of the program.

    Any easy way around this?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Well, the way around this is to fix what you're doing wrong. I guess if you've got an array on the heap, you used new[]? If so, use delete[] to delete it. Or if you leak your memory you should fix that. Without more information, I don't know what's gone wrong.

  3. #3
    Registered User
    Join Date
    May 2007
    Posts
    27
    The declaration goes like this:
    Code:
    GameSprite* pSpritePointer=new GameSprite[4];
    Originally, I had attempted to delete it like this:
    Code:
    if(pSpritePointer)
        {
        delete pSpritePointer;
        }
    That generated the debug assertion error. However, I did what you said by using

    Code:
    delete[4] pSpritePointer;
    and it worked fine. Like I said I never was in a situation where i wanted to use it before so I didnt know you could use delete[]. Thanks for the tip!

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Just delete [] pSpritePointer; not delete[4] pSpritePointer;
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,641
    Quote Originally Posted by DarkMasterBosel
    Any easy way around this?
    #include <vector> and use a std::vector. Read Stroustrup's answers to the FAQs: How do I deal with memory leaks? and What's wrong with arrays? (though the latter has fixed size arrays as its focus).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 12:01 AM
  2. Deleting an element from an array
    By Matt13 in forum C Programming
    Replies: 7
    Last Post: 11-19-2003, 03:42 AM
  3. Managing a heap using an array.
    By rahuls in forum C Programming
    Replies: 3
    Last Post: 03-20-2003, 01:49 PM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  5. Deleting from an array
    By Dargoth in forum C++ Programming
    Replies: 5
    Last Post: 02-06-2002, 02:56 PM

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