Archived

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

memory management using vectors

This topic is 5148 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

In my game, I have an EnemyManager class and an Enemy class. The EnemyManager currently has a static array of Enemies, so there''s a maximum number of Enemies, and memory is allocated for them when the EnemyManager is constructed. cEnemy m_Enemies[MAX_ENEMIES]; -> memory allocated for MAX_ENEMIES cEnemy objects When the script adds an Enemy object to the game, an already existing Enemy object is simply initialized - no memory allocation is necessary at this point. Now I want to change this static array to a vector of cEnemy objects: vector m_Enemies; and my question is how to add enemies now. When the script is parsed and a new Enemy should be created, it should not only be initialized, but also constructed and pushed_back on the vector, but I wouldn''t know how to construct the enemy object from the script parser because the enemy object would go out of scope and be deleted (i guess). cEnemyManager:arseScript(...) { //..... cEnemy Enemy = new cEnemy; Enemy.Init(...); m_Enemies.push_back(&Enemy) //..... } -> here the Enemy goes out of scope, is deleted, and the vector is messed up. How and where should I allocate memory for the cEnemy object to be able to push it on my vector and keep it working? (The only vector examples I can find googling are int and string vectors, and none deal with vectors of custom classes)

Share this post


Link to post
Share on other sites
If you create an enemy instance with new and push_back the pointer only that pointer var gets out of scope. The enemy object itself stays.

The way you do is almost right, the line

cEnemy Enemy = new cEnemy;

should read:

cEnemy* Enemy = new cEnemy;

and it should work like you want.

The drawback is, you need to delete the pointers in the vector when closing yourself.

Share this post


Link to post
Share on other sites
Ah thanks,

for one reason or another I keep going back to the thought that once the ''original pointer'' to an object is destroyed, the object itself is destroyed as well.. but the object is only destroyed when it is actually deleted then?

So when Enemy objects are destoyed, they should be deleted first, and then removed from the vector. Right?

Share this post


Link to post
Share on other sites
Yep, right.

Object that were newed are only destroyed when calling delete on them (the same with new[] and delete[]).

So when you accidentally remove an object pointer before deleting the object you can''t delete it anymore (since you "lost" the pointer to the object).

Share this post


Link to post
Share on other sites
Just so you know, vector copies the object you push back (ie it won''t go out of scope if you don''t create it with new). ie:

vector<Bullet> bullets;
//...

int function()
{
Bullet bullet(0,0);
bullets.push_back(bullets);
}

However, it''s usually not a good idea to do that with medium to large sized classes because it will take a long time (relative to passing a pointer) to add.

Share this post


Link to post
Share on other sites