Skip to main content
gamedev.net
🔒 Locked

new/malloc

Started by IO Fission 15 replies 750+ views
Original Post
IO Fission
IO Fission
I''m a C programmer still trying to get used to C++. Is there any difference between the memory the new allocates and the memory that malloc allocates, or are they pretty much the same. Also, if I''m programming Win32, should I use windows memory functions, or doesn''t it really matter? Thanx - IO Fission Tearing 'em apart for no particular reason...
gimp
gimp
But what I''ve never found a replacement for in C++ is realloc... Just say I want my char[64] to expand out to char[140]...

Of course this is only mild curiosity as I use basic strings and vectors for that now...but still...

igimp
Chris Brodie
RWarden
RWarden
New is the preferred way of doing things in C++ because when you create a class using new, it''s constructor is called. And when you delete a class using delete, it''s destructor is called. These functions aren''t called when you use malloc/free.

-RWarden (roberte@maui.net)
Erik Post
Erik Post
Something similar to realloc must be done manually: first create the new storage, copy all the elements, and then delete the old storage and finally use the address of the old storage. Realloc essentially does the same.

But, since you already use the STL stuff, realloc isn''t really needed, except (maybe?) for low-level things.

Erik
ImmaGNUman
ImmaGNUman
Using new and delete would just be better style in C++, unless of course you are old school like me.

-----------------------------

A wise man once said "A person with half a clue is more dangerous than a person with or without one."
-----------------------------A wise man once said "A person with half a clue is more dangerous than a person with or without one."The Micro$haft BSOD T-Shirt
mhkrause
mhkrause
The heap functions (HeapCreate, HeapAlloc, HeapFree, HeapDestroy) are the "preferred" ways of allocating memory under Win32. The C/C++ allocaters use these internally. You may gain a minor performance increase if you use these functions instead of the new/malloc.

Remember, you can globally override operator new to use whatever allocation scheme you choose.
IO Fission
IO Fission
Ok, so from what I''ve gathered they all get the same memory anyway, just using different funcitons to end up calling the same thing (including the constructors/destructors part for new).

Thanx all for helping to clarify that!


- IO Fission

Tearing 'em apart for no particular reason...
Gandalf
Gandalf
When is it correct to use delete[]?

If I create a float vector,


float *data;
data = new float[32*32*3];


is it correct to destroy this memory with delete[]?
or should it be just delete.

Thanks,




Gandalf the White

You'll find a golden ring...


Edited by - Gandalf on July 7, 2000 8:14:06 AM
Gandalf the Black
Erik Post
Erik Post
Memory allocated with new[] (like in the example given above) should be de-allocated with delete[], and memory allocated with new should be de-allocated with delete. Otherwise, things will go wrong.

Erik
Gandalf
Gandalf
Thanks, I thought so.

Gandalf the White

You'll find a golden ring...
Gandalf the Black
furby100
furby100
How do you allocate an array of structures using new? The way with malloc is malloc(num_elements * sizeof(structure)), but I am interested to know how it is done with new.

I prefer malloc, as it is closer to what the computer actually does.

------------------------------
#pragma twice
Wilka
Wilka
furby100:

structure* pStruct = new structure[num_elements];

remember to use delete[] pStruct when your done. Or you could use vector, then you wont have to worry about deleting or realloc''ing anything.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.