Sign in to follow this  
BloodLust666

pointers

Recommended Posts

yea, i forgot.. if i allocated memory for one pointer, how do i set aside another allocation without interfering with the first? cType *ptr = new cType(); // do stuff ptr = new cType(); // do stuff also (so no one thinks i'm just leaving the memory there) i have a vector<> template which i store the points in, then i want to allocate new memory but use the same name.. can i do that?

Share this post


Link to post
Share on other sites
When you store pointers in a std::vector<>, you still must manually delete the objects to which they point to; std::vector<> will not do this for you (and you wouldn't want it to). Iterate through and delete, then you can assign or new more objects as desired.


std::vector< int * > v( 4, new int( 20 ) );
// ...
std::vector< int * >::iterator it = v.begin();
while( it != v.end() ) delete *it++;

Share this post


Link to post
Share on other sites
i know that, that's all done within the engine. in the actual program, here's what it looks like:

cTexture* pTex = W_TextureMapList.Add("Image");
pTex->Load("Star_Field.bmp");

pTex = W_TextureMapList.Add("Ship");
pTex->Load("Ship.png");

the Add() (which is actually a map<> template) adds a "new" cTexture with the "string" in the parameter and returns the "new" pointer.
then i want to create a new one with the same name without having to initialize another pointer.

Share this post


Link to post
Share on other sites
know what i mean? internally in the engine there's of course the call to a "new" type and in the .Shutdown() it scrolls through all the items in the map list and deletes them all. but for some reason when i do what i'm doing, i get an error in the heap thing...

Share this post


Link to post
Share on other sites
Quote:
Original post by EvilKnuckles666
know what i mean? internally in the engine there's of course the call to a "new" type and in the .Shutdown() it scrolls through all the items in the map list and deletes them all. but for some reason when i do what i'm doing, i get an error in the heap thing...

Posting a little code ( your .shutdown() function and/or any code that refers to your map after .shutdown() ) might help, because it definitely sounds like you're trying to use bad pointers somewhere.

Share this post


Link to post
Share on other sites
here's the shutdown code for the template

[source language="cpp"]
template <class Type>
void tMapList<Type>::Shutdown()
{
if (m_MapList.size() > 0)
{
map <string, Type*>::iterator Iter;
for ( Iter=m_MapList.begin() ; Iter!=m_MapList.end() ; Iter++)
{
delete Iter->second;
Iter->second = NULL;
}
m_MapList.clear();
}
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this