new / calloc

Started by
11 comments, last by Zeusel 22 years, 3 months ago
yeah, ok my bad. I should have said you need to use free(). The point that i was making was that i'd forget to use delete with all new's, i think. I've never used them but new seems like a far easier method of declaring memory and i'd forget (yes, i really am that stoopid).

As for the extra memory bit (i'm totally guessing here) doesn't new delete memory when the variable goes out of scope? So it needs to keep a record of the memory it's assigned AND their scope. Like i said, i've never used it or been that tempted to so i could be completely off base here but it's just a hunch.

Actually, thinking about it, deleting memory just cos it's gone out of scope would be stupid. Ignore everything i've just said.

zipless

EDIT: Hang on i was right the first time, sorta. What i should have said was new deletes the memory when the variable goes out of scope. yes? No? Aw screw it, i'll go read about it and come back when i know what i'm talking about.


Edited by - zipless on January 14, 2002 6:54:23 AM

Edited by - zipless on January 14, 2002 6:57:47 AM
/* Ignorance is bliss, then you go and spoil it by learning stuff */
Advertisement
No, memory allocated by new is not deleted when it goes out of scope. If you want that kind of behaviour, put it into a destructor of some sort (potentially an auto_ptr, if you''d like that).

[Resist Windows XP''s Invasive Production Activation Technology!]
The power of new is the 2 extra tihngs it brings to the table. First, it knows what it is allocating memory for, so it returns a pointer to the correct type. Calloc/malloc return a pointer to void that has to be cast to the correct type. The other thing new does for you is to call the constructor of an object, if that is what you are calling new for. If you are using objects, you cannot us malloc, because the constructor would not be called.

---
Make it work.
Make it fast.

"Commmmpuuuuterrrr.." --Scotty Star Trek IV:The Voyage Home
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][LWJGL Tutorials for NeHe][LWJGL Wiki][jMonkey Engine]

This topic is closed to new replies.

Advertisement