memory management using vectors
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)
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.
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.
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?
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?
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).
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).
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:
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.
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement