Recommended Posts

What exactly does the new command do? I have a particle system that I want to have a variable number of particles, so in a structure I have a pointer to the particles, then when I setup the system I say that pointer =new PARTICLE[numberofparticles].
This crashes if I start the fountain too many times, and I''m not sure why, but I think I have to "free" the particles. Is this allocating memory? And how do I "free" a custom structure?
______________________________
To make sure teen mothers don''t just dump their babies in trash cans-
We''re going to let them kill them instead.

0

Share this post

Link to post

Share on other sites

What you are doing by that line is creating a new array on the freestore with the size that you need, the name of the array is a pointer to the start of the array, when you are done with this though you need to call delete on the array like this

delete [] pointer;

to remove the whole array from the array then set the pointer to null,

Link to post

Share on other sites

so it answers your questions i guess...just usedelete []pointer; for you case

THERE IS ONE BIG DIFFERENCE with new/malloc-delete/freedon't forget (if you knew already) that MALLOC ONLY allocatesa block of memory and returns the start address...now the NEW operator(and NOT "COMMAND", gee) allocates memory and THEN CALLS the constructor of the object!!! it's really important to distinguish between the too..same goes for free(mem only) and delete (call dtor and free-mem)

now the way NEW operator allocates memory is irrelevant...or doesn't matter...if you really one know you can do it yourself with operator NEW...which is the function which really allocates memory...

that's it

btw-when you call new call delete and when you call new [] call delete[]by now i can be sure you know the difference already

Share this post

Link to post

Share on other sites

Actually if you''re writing a particle system the way it''s usually done is with an array. If a particle "dies" you instantiate a new one in the empty array. This will eliminate memory problems and it will increase your performance because new and delete take up valuable time.