Sign in to follow this  

Deleting individual elements of dynamically allocated array

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

Hi, I'm currently working on a small game in C++ to get up to speed on coding again, which I hasn't really done to any serious extent for a while. This game uses particle effects, and the particle engine has a method Spawn(int num, PARTICLE &base, PARTICLE &deviation) which spawns an arbitrary number of random particles, based on the values in base with a maximum deviation from these values of deviation. To speed this method up a bit, I've considered allocating all new particles using PARICLE *p = new PARTICLE[num]; instead of allocating each particle individually. However, these particles will all need to be deleted individually later on. This brings us to the question: is it OK to do this? 'cause I kind of get the feeling that it isn't. PARTICLE is a struct (no methods, no constructors, no destructors, only data members), so there are no object intricacies involved.

Share this post


Link to post
Share on other sites
Quote:
Original post by Antheus
std::vector< PARTICLE >


Or...

typedef boost::shared_ptr< PARTICLE > ParticlePtr;
typedef std::vector< ParticlePtr > Particles;
Thing is, these PARTICLEs are going into a linked list later on, so stuffing them in a std::vector seems a bit overkill.

Actually, I just found the answer to the question myself: it's definitely not OK. Caused an "alread freed" exception on deletion. Thanks anyway.

Share this post


Link to post
Share on other sites
Quote:
Thing is, these PARTICLEs are going into a linked list later on, so stuffing them in a std::vector seems a bit overkill.


std::list?



Quote:
Actually, I just found the answer to the question myself: it's definitely not OK. Caused an "alread freed" exception on deletion. Thanks anyway.


Are you running in debug mode?

Share this post


Link to post
Share on other sites
Why are they going in a linked list?

Anytime you feel yourself managing memory using new[] and delete[] then prefere to use a vector.

I would redesign your system so you arent individually deleting particles, already its caused you a memory management problem, but its not good for performance either.

Something like this is perhaps better:

typedef std::vector< PARTICLE > ParticlePool;

ParticlePool particlePool; // A pool of particles, dead or alive

std::vector< ParticlePool::size_type > liveParticles; // Indexes to live particles
std::vector< ParticlePool::size_type > deadParticles; // Indexes to dead particles

// Allocate particles but dont construct them
particlePool.reserve( allocSize );

// Also allocate memory for dead and live particle lists
liveParticles.reserve( allocSize );
deadParticles.reserve( allocSize );


/*
Spawning a particle
*/


// Is there a dead particle we can use?
if( !deadParticles.empty() )
{
PARTICLE& particle = particlePool[ deadParticles.back() ]; // Grab a dead particle
initialiseParticle( particle ); // Initialise the particle
liveParticles.push_back( deadParticles.back() ); // Add the index to the live-list
deadParticles.pop_back(); // Remove index from the dead-list
}
else // There are no dead particles so add a new one
{
PARTICLE particle; // Construct a particle
initialiseParticle( particle ); // Initialise it
liveParticles.push_back( particlePool.size() ); // Add the index to the live-list
particlePool.push_back( particle ); // Add the particle into the pool
}


This way you allocate memory only once unless you didnt allocate enough to begin with (in which case the vector will have to re-allocate but fortunately this is taken care of transparently). All the particles are stored in contiguous memory for fast access and we have a list of indices to live and dead particles so particles that die arent deleted but re-used when new ones are needed.
This will be far more efficient as it doesnt require constant allocation and de-allocation of tiny particles.

[Edited by - dmatter on August 12, 2007 12:23:15 PM]

Share this post


Link to post
Share on other sites

This topic is 3781 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.

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