Archived

This topic is now archived and is closed to further replies.

Freeing Memory

This topic is 6288 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Please take a look
            
class Sprite
{
LPDIRECTDRAWSURFACE7 surface;
};
Sprite::~Sprite()
{
surface->Release();
}

Sprite * mySprite[5];
int i;
for (i = 0; i < 5; i++)
{
mySprite<i> = new Sprite();
}

for (i = 0; i < 5; i++)
{
delete mySprite<i>;
}
            
Would this free up all the memory that the Sprite take up? Edited by - vbisme on 10/30/00 9:06:32 PM Edited by - vbisme on 10/30/00 9:44:34 PM

Share this post


Link to post
Share on other sites
when you say :

mySprite = new Sprite();

you are assigning the pointer to mySprite[0] to point to a new Sprite (this screws up the array) What you probably want to do is create an array of sprite pointers and allocate them individually. e.g.

    
Sprite *mySprite[5]
for (int i=0;i<5;i++)
mySprite<i> = new Sprite();

// then to deallocate

for (i=0;i<5;i++)
delete mySprite[i];



Brought to you by: O.G.A.P.O.
+----------------------------------------+
| Surgeon General's Warning - |
| OGAPO can cause serious mental damage |
| if taken in large doses. |
+----------------------------------------+

/* Never underestimate the power of stupid people in large groups */

Share this post


Link to post
Share on other sites
It should actually look like that:
            
class Sprite
{
LPDIRECTDRAWSURFACE7 surface;
};
Sprite::~Sprite()
{
surface->Release();
}

Sprite * mySprite;

mySprite = new Sprite[5]; //it only works using default constructor


delete [] mySprite;




Edited by - vbisme on 10/30/00 9:06:32 PM

Share this post


Link to post
Share on other sites