pointers

Started by
8 comments, last by BloodLust666 18 years, 6 months ago
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?
-------------------------Unless specified otherwise, my questions pertain:Windows Platform (with the mindset to keep things multi-platform as possible)C++Visual Studio 2008OpenGL with SFML
Advertisement
what are you really trying to do? I mean, what part of your game are you making? Chances are that you aren't asking the right question.
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++;
:stylin: "Make games, not war.""...if you're doing this to learn then just study a modern C++ compiler's implementation." -snk_kid
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.
-------------------------Unless specified otherwise, my questions pertain:Windows Platform (with the mindset to keep things multi-platform as possible)C++Visual Studio 2008OpenGL with SFML
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...
-------------------------Unless specified otherwise, my questions pertain:Windows Platform (with the mindset to keep things multi-platform as possible)C++Visual Studio 2008OpenGL with SFML
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.
:stylin: "Make games, not war.""...if you're doing this to learn then just study a modern C++ compiler's implementation." -snk_kid
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();	}}
-------------------------Unless specified otherwise, my questions pertain:Windows Platform (with the mindset to keep things multi-platform as possible)C++Visual Studio 2008OpenGL with SFML
wth... now my break points won't work when i put them in the shutdown functions, even at the very beginning an error hasn't hit yet.
-------------------------Unless specified otherwise, my questions pertain:Windows Platform (with the mindset to keep things multi-platform as possible)C++Visual Studio 2008OpenGL with SFML
y is there a grey question mark inside my breakpoints??? o.O
-------------------------Unless specified otherwise, my questions pertain:Windows Platform (with the mindset to keep things multi-platform as possible)C++Visual Studio 2008OpenGL with SFML
wow... figured it all out... somethign wrong internally (too much to post) and i forgot i was in release mode
-------------------------Unless specified otherwise, my questions pertain:Windows Platform (with the mindset to keep things multi-platform as possible)C++Visual Studio 2008OpenGL with SFML

This topic is closed to new replies.

Advertisement