vector<CParticle*> m_ParticleList;
/////////////////
I have an ExplosionSystem that is derived from ParticleSystem, and this is where the particles are allocated eg:
/////////////////
CParticle* particle;
particle = new CExplosionParticle(...);
m_ParticleList.push_back(particle);
////////////////////////////
my Question: Is the following the correct way to deallocate the memory used by each particle? (Using []? )
Do I need to do anything to the vector itself as well?
////////////////////////////
CParticleSystem::~CParticleSystem()
{
for(int i=0;i < m_ParticleList.size();i++)
{
delete [] m_ParticleList(i); //pretend these are square braks
m_ParticleList<i> = NULL; // and these ones too
}
}
Thanks.
Edited by - gumball on 9/6/00 3:51:38 AM
Edited by - gumball on 9/6/00 3:52:18 AM
deallocating memory using []
Hi,
I have a ParticleSystem that maintains a vector of particles like so:
No, it is not correct.
Only use delete[] if you have a pointer to a C-style array. If you have Stroustrup''s C++ book 3rd Edition this is stated on page 128.
What you have a pointer to is a CExplosionParticle, and that is not a pointer to a C-style array so don''t use [] with it. Use regular delete instead.
Even if you have a pointer to std::vector you still only use delete on it, because a std::vector is not the same as a C-style array - although it internally may be implemented as such.
Jacob Marner
Only use delete[] if you have a pointer to a C-style array. If you have Stroustrup''s C++ book 3rd Edition this is stated on page 128.
What you have a pointer to is a CExplosionParticle, and that is not a pointer to a C-style array so don''t use [] with it. Use regular delete instead.
Even if you have a pointer to std::vector you still only use delete on it, because a std::vector is not the same as a C-style array - although it internally may be implemented as such.
Jacob Marner
Jacob Marner, M.Sc.Console Programmer, Deadline Games
Quick and easy answer... No, only use delete[] if you previously used new[].
---- --- -- -
Blue programmer needs food badly. Blue programmer is about to die!
---- --- -- -
Blue programmer needs food badly. Blue programmer is about to die!
No, you should do something like:
CParticleSystem::~CParticleSystem(){ // for every element in the particle vector vector<CParticle*>::iterator iter = m_ParticleList.begin(); while(iter != m_ParticleList.end()) { // deallocate memory pointed by CParticle* delete *iter; // zero out pointer to the memory we''ve just deallocated // it''s good habit, ensures that you won''t use a dangling // pointer by accident *iter = NULL; iter++; } // finally purge all elements from the vector m_ParticleList.clear()}
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement