views:

1165

answers:

9

Duplicate of: In what cases do I use malloc vs new?

Just re-reading this question:
http://stackoverflow.com/questions/807939/what-is-the-difference-between-new-and-malloc-and-calloc-in-c-closed

I checked the answers but nobody answered the question:

  • When would I use malloc instead of new?

There are a couple of reasons (I can think of two).
Let the best float to the top.

A: 

A C++ programmer should rarely if ever need to call malloc. The only reason to do so that I can think of would be a poorly constructed API which expected you to pass in malloc'd memory because it would be doing the free. In your own code, new should always be the equal of malloc.

Steve Rowe
+9  A: 

A couple that spring to mind:

  • When you need code to be portable between C++ and C.
  • When you are allocating memory in a library that may be called from C, and the C code has to free the allocation.
anon
+1  A: 

The best reason I can think of to use malloc in C++ is when interacting with a pure C API. Some C APIs I've worked with take ownership of the memory of certain parameters. As such they are responsible for freeing the memory and hence the memory must be free-able via free. Malloc will work for this puprose but not necessarily new.

JaredPar
+1  A: 

One of the reason is that in C++, you can overload the new operator.

If you wanted to be sure to use the system library memory allocation in your code, you could use malloc.

Benoit
You could also call ::new couldn't you?
Steve Rowe
You can never *really* be sure (eg. you could have preloaded your libc to call you malloc/free), and you shouldn't write code that has to care.
Greg Rogers
Not to mention that you can override the global new.
Michael Burr
A: 

If the memory is to be released by free() (in your or someone elses code), it's pretty darn required to use malloc.

Otherwise I'm not sure. One contrived case is when you don't want destructor(s) to be run on exit, but in that case you should probably have objects that have a no-op dtor anyway.

Marcus Lindblom
+2  A: 

In C++, just about never. new is usually a wrapper around malloc that calls constructors (if applicable.)

However, at least with Visual C++ 2005 or better, using malloc can actually result in security vulnerabilities over new.

Consider this code:

MyStruct* p = new MyStruct[count];
MyStruct* p = (MyStruct*)malloc(count* sizeof(MyStruct));

They look equivelent. However, the codegen for the first actually checks for an integer overflow in count * sizeof(MyStruct). If count comes from an unstrusted source, it can cause an integer overflow resulting in a small amount of memory being allocated, but then when you use count you overrun the buffer.

Michael
If you dont do any sanity checking on count coming from an untrusted source, you have a security vulnerability whether you use malloc or new.
Greg Rogers
This is just another defence layer, it definitely doesn't allow you to blindly trust data.
Michael
the second example should be using calloc in C, it will do an overflow check: MyStruct *p = calloc(count, sizeof(MyStruct));
+4  A: 

From the Stroustrup FAQ on new/malloc I posted on that thread:

Whenever you use malloc() you must consider initialization and convertion of the return pointer to a proper type. You will also have to consider if you got the number of bytes right for your use. There is no performance difference between malloc() and new when you take initialization into account.

This should answer your question.

dirkgently
To be honest, I dont see how.
anon
A: 

You can use malloc when you don't want to have to worry about catching exceptions (or use a non-throwing version of new).

jeffamaphone
+2  A: 

Everybody has mentioned (using slightly different words) when using a C library that is going to use free() and there are a lot of those around.

The other situation I see is:

When witting your own memory management (because for some reason that you have discovered through modeling the default is not good enough). You could allocate memory block with malloc and the initialization the objects within the pools using placement new.

Martin York
Why not allocate the raw memory with new?
anon
void* buffer = new char[100000]; is perfectly legal c++
anon
If you want raw memory, the equivalent to malloc would be operator new, e.g. void* buffer = ::operator new(100000); Although char's are basic types so don't do anything dangerous on construction or destruction, it's semantically more clear to use operator new than to use a char array object and then reuse its underlying storage for a different set of objects.
Charles Bailey
Yep those are both good alternatives to malloc. Before I actually used malloc personally I would need to show (through measurement) that malloc was significantly faster (because the only reason I would ever even be doing the memory management is that I needed some speed).
Martin York