Archived

This topic is now archived and is closed to further replies.

hooded_paladin

the "new" command

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.

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,

Ballistic Programs

Share this post


Link to post
Share on other sites
Yes, new allocates memory. You must delete it when done.

If it crashes during a new, it means you probably ran out of memory. This will happen if you''re not ''delete[]''ing it when you''re done. Each time you''re grabbing more memory.

Share this post


Link to post
Share on other sites
That''s what I thought.
So, if I have a struct with a pointer to it, do I go:
A) delete[Fountain]
B) delete[Fountain.particle[loopthroughtheparticles]]
C) delete[Fountain.particle]
or what?

Share this post


Link to post
Share on other sites
you must delete/free pointers not data types..

say
int* x;
x = new int;
delete x;
and NOT delete int;

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

THERE IS ONE BIG DIFFERENCE with new/malloc-delete/free
don't forget (if you knew already) that MALLOC ONLY allocates
a 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

farewell

Arkon
[QSoft]

[edited by - Arkon on March 22, 2002 3:01:32 PM]
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.

Share this post


Link to post
Share on other sites
But it''s a pointer to an array-
I only have to do new and delete once...

______________________________
To make sure teen mothers don''t just dump their babies in trash cans-
We''re going to let them kill them instead.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
new and delete do not take up valuable time at all. Adding two numbers takes up more time thankyou.

Share this post


Link to post
Share on other sites
quote:
Original post by Anonymous Poster
new and delete do not take up valuable time at all. Adding two numbers takes up more time thankyou.

Er, no. OS memory management routines and operator new/delete do a lot more than add two numbers.

Share this post


Link to post
Share on other sites
I fought the whole new/delete thing when I first started with C++. The way I figured out what to do was simply trace through it with a debugger - not a lot can be hidden there.

Speedwise - new and delete are pretty quick. What is not quick is instantiating all of the stuff in your class and running through your constructor. However I am sure they are not faster then add ( about 2 machine cycles??).

I agree with Ataru - if you want to keep it fast avoid constructing / deconstructing objects repeatedly. The best approach would be to allocate a "heap" and use/reuse the same space over and over again. This means you will need to do a little more work on the management of your data but you should see a decent speed improvement. Also - if you do not need to grow or shrink that ''array'' you may be able to keep the memory contiguous for other optimizations.

Good luck



Share this post


Link to post
Share on other sites