Archived

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

Zeusel

new / calloc

Recommended Posts

zipless    122
They should be exactly the same althought i expect that new may use up a bit more memory, keeping track of what you''ve new''d.

I personally use alloc because i''m used to it, i know if i start using new half way through a project i''ll keep forgetting to delete(?) them. Then again it does seem like a far neater option.

Share this post


Link to post
Share on other sites
Null and Void    1088
The calloc function is just like malloc except that it zero''s out the memory at the same time (this is unneeded most of the time, and lowers the speed). If you''re using C++ just stick with new and delete.

quote:
Original post by Prosper/LOADED
since C is not OO

Neither is C++. C++ just has added capabilities for working in an OO paradigm. Therefore, C++ is multiparadigm, not OO. OO code is still very possible in C (look at GTK+).

[Resist Windows XP''s Invasive Production Activation Technology!]

Share this post


Link to post
Share on other sites
hewhay    122
quote:

I personally use alloc because i'm used to it, i know if i start using new half way through a project i'll keep forgetting to delete(?) them. Then again it does seem like a far neater option.



Doesn't you need to call free after calling malloc?

Edited by - hewhay on January 13, 2002 4:27:04 PM

Share this post


Link to post
Share on other sites
Fruny    1658
quote:
Original post by zipless
They should be exactly the same althought i expect that new may use up a bit more memory, keeping track of what you''ve new''d.



So does malloc().

Share this post


Link to post
Share on other sites
zipless    122
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

Share this post


Link to post
Share on other sites
CaptainJester    523
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

Share this post


Link to post
Share on other sites