Do you actually use dynamic memory?

Moderator
Posts: 1,140
Joined: 2005.07
Post: #31
copyData does just that: it copies all the variables from the Particle in the parameter to the particle that it's called from. It should be declared as
void copyData(Particle &particle)
and just have a bunch of lines such as data = particle.data. If you have arrays, be sure to copy the elements in a loop.

To clarify the necessity, since you have an array of Particle objects in ParticleSystem, they all call the default constructor, so you will need to copy the data after the fact. (hence this method)

Edit: to clarify, this is supposed to be a member function of Particle.
Quote this message in a reply
Sage
Posts: 1,066
Joined: 2004.07
Post: #32
Ok, just one C++ question: what does this line do? I'm confused by the : and ? and < in the middle of it.
Code:
workingSize = (numParticles < particleSize) ? numParticles : particleSize;
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #33
Code:
x = b ? m : n;
is exactly equivalent to
Code:
if (b)
{
    x = m;
}
else
{
    x = n;
}
Quote this message in a reply
Sage
Posts: 1,066
Joined: 2004.07
Post: #34
Cool. Thanks OSC.
Quote this message in a reply
Moderator
Posts: 3,573
Joined: 2003.06
Post: #35
The conditional operator ? was originally a C feature I believe. It's often cryptic to read, and it's easy to write pointless code with too, so it's usually best to avoid using it. Nice to know when you need to read other people's code though.
Quote this message in a reply
Moderator
Posts: 1,140
Joined: 2005.07
Post: #36
I find it preferable for simple things such as that, since it's much quicker and uses less space than an if statement. If you get into complicated tertiary statements, it can get ugly, though.

I think the tertiary operator may have originated before C, possibly with Fortran or before. IIRC, it was the only choice for a while. (not 100% sure about that, though)
Quote this message in a reply
Moderator
Posts: 771
Joined: 2003.04
Post: #37
Someone should split this thread.
Quote this message in a reply
Moderator
Posts: 133
Joined: 2008.05
Post: #38
Just a tip on you're particle code as it stands now...

As you know, a particle system constantly is losing particles and adding new particles, and doing this rather quickly depending on what your particle system is trying to represent. If you were to try that on your current code, your computer would probably explode.

Why? Well, say you have so many particles and you add a couple hundred more, you are copying over an entire array, deleting an entire array, allocating a new larger array, copying over an entire array then deleting an entire array... a lot of times. I know this thread is about dynamic memory, but this is a textbook example of when NOT to use dynamic memory.

Also, I'm not sure why you need a display list for every single one of your particles, a billboarded quad is the best way to do it, that way you aren't pushing/popping the modelview matrix a billion times a frame, and calling scaling/translating functions each particle, each frame.

Furthermore, you are allowed to use [] on dynamically allocated arrays. I'm not sure on what kind of speed differences you get by using the [] operators vs. incrementing the pointer(+ function overhead of sizeof), maybe OSC or someone else at that level can speak on that one, I know I'd certainly like to know which is quicker. If you're already using an incrementing variable to loop thru the number of particles, might as well use the [] operators at least to make it a bit more clear to yourself while learning.
Quote this message in a reply
Moderator
Posts: 1,140
Joined: 2005.07
Post: #39
I'd imagine the difference using [] would be negligible at best. Using particles[i] should be the same as doing particles + i. So basically, it would be like the difference between i + 1 and i++ numerically. Also, sizeof isn't a function, it's technically an operator. If you wanted to be a stickler, there is also a difference between ++i and i++ or ++particles and particles++, since post incrementation also creates a temporary variable. (why I always use ++i instead of i++ now Wink) Essentially, it's nothing you'd notice unless you had about 20 billion particles or so, and even then 1 second out of an hour isn't going to be very noticeable. Wink (numbers completely made up, but you get the idea Rasp)
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Troubles With Dynamic Memory Nick 1 2,446 Sep 7, 2005 08:56 PM
Last Post: Nick