Jump to content
  • Advertisement
Sign in to follow this  
mrmrcoleman

Filling a vector..

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

Just on quick question about the correct way to add objects to an STL vector... In my particle system I have a vector which stores all the particles. This is initially empty and the particles are added at a uniform rate up to a maximum number allowed. Here's some psuedo code:
if timer states that it is time to start another particle
    if the size of the vector is not greater than the maximum permissable particles
         initialize and new particle and add it to the vector
    end if size
end if timer

My question is to do with the central part. Is this the correct way to add a new particle object:
// This is the particle struct
struct Particle
{
   Vector currentposition;
   Vector currentvelocity;
   // etc etc
};

if(ParticleList.size() <= MaxParticles)
{
   Particle newParticle = new Particle;
   InitialiseParticle(&newParticle);
   ParticleList.push_back(Particle);
}

// Where Particle list is defined as
vector<Particle> ParticleList;

This seems right to me, but I had a few reservations about where the particle that is allocated with new would end up when we exit the function... Would this be lost memory, or is this ok? Thanks in advance for any help. Kind regards. Mark Coleman

Share this post


Link to post
Share on other sites
Advertisement
Adding "new"ed elements to the vector is perfect okay. Nothing will happen to them once you leave the function. The only thing is that when you want to remove a particle you will have to delete it as well as remove it from the vector.

EDIT: I missed the definition of ParticleList. jamessharpe is right.

Share this post


Link to post
Share on other sites
You're not actually storing pointers to particles in the vectors so there is no need to call new. In fact the code you posted will generate a compiletime error on the line:


Particle newParticle = new Particle;


Since new Particle will return a Particle *. As long as everything contained within the particle class has appropriate copy constructors then you will be fine just declaring the particle without calling new:


Patricle newParticle;
InitialiseParticle(&newParticle);
ParticleList.push_back(newParticle);



James

Share this post


Link to post
Share on other sites
That won't even compile. operator new returns a pointer, which can't be assigned to a stack allocated instance. Additionally you're not actually doing anything with newParticle. You allocate it (incorrectly) and initialise it and then do nothing more with it, pushing a new default constructed Particle into the container instead. You either want:
std::vector<Particle> ParticleList;
// ...
Particle newParticle;
InitialiseParticle(newParticle); // use reference semantics instead of taking the address
ParticleList.push_back(newParticle);

or
std::vector<ParticlePtr> ParticleList;
// ...
ParticlePtr newParticle = new Particle;
InitialiseParticle(newParticle);
ParticleList.push_back(newParticle);

In the second case you'll need to explicitly delete each Particle before you remove it from the container. ParticlePtr here is a typedef for your smart or not-smart pointer of choice.

Enigma

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!