Is there any good reason to use 'malloc' in C++, when there is 'new'? [duplicate]
Possible Duplicate:
开发者_StackOverflow In what cases do I use malloc vs new?
Duplicate of: In what cases do I use malloc vs new? and When to use Malloc instead of New
Does anyone have real life programming scenarios where using "malloc" instead of "new" in C++ was justified?
To new is C++;
To malloc is C;
To mix them is sin.
There is no very good reason to do so and this question is very much related to this one.
new
always invokes a constructor (although in the case of PODs something else happens). Sometimes – very rarely – you want to deal with raw memory, not associated with any type. This can be the case when you’re implementing your own allocator
class. Even then, malloc
is rarely the right thing to do but sometimes you may want to take advantage of realloc
i.e. be able to resize the underlying storage more efficiently. This is one scenario that would require malloc
to get the initial storage.
Another actual use-case for raw memory is when you find yourself implementing a “non-predictable” pseudo-random number generator (don’t!). In order to increase the entropy available to the generator, you might use uninitialized memory as a basis for the random seed. Entropy from various sources in the hardware is crucial for such operations so using uninitialized memory (about which not many predictions can be made) can be desirable, if you know exactly what you’re doing.
For completeness’ sake I should point out that the same can be achieved by calling ::operator new
instead of malloc
. The latter also does some checking to see whether the required amount of memory could be allocated successfully and invokes the appropriate callback handlers if something goes wrong (cf. _set_new_handler
). Last but not least, ::operator new
will throw std::bad_alloc
if no handler manages to free enough memory (unless std::nothrow
was specified as the second argument, in which case 0
will be returned).
The only example of a somewhat justified use of malloc in C++ is in the creation of a hand-rolled memory allocation scheme where a large chunk of raw memory is allocated, and then small objects are created within this buffer using placement-new.
However even this use is questionable, as the same goal can and probably should be achieved by using either new or operating-system-provided allocation functions to create an initial large buffer of char, then placement-newing objects within that.
Here are some compares: http://www.velocityreviews.com/forums/t288250-malloc-vs-new.html
Realloc may only be used on memory locations that are malloced. Realloc prefereably resizes a memory block instead of allocating a new one. Memory allocation is relatively expensive in C/C++ so this could be an advantage in performance.
The most obvious scenario would be integration with a legacy system the requires you to pass or accept ownership of some memory as you shouldn't mix new/free or malloc/delete.
I have never used malloc from C++
精彩评论