Possible Duplicate:
What is the difference between new/delete and malloc/free?
Hi guys i am a noob in c++, want to know whether
memblock = (char *)malloc( currentByteLength);
is equivalent to
memblock = new char[currentByteLength]
in c++.
memblock = (char *)malloc( currentByteLength);
memblock = new char[currentByteLength];
No difference now. But if you replace char
with int
, then yes, there would be difference, because in that case, malloc
would allocate memory of size currentByteLength
, while new
would allocate memory of size size(int) * currentByteLength
. So be very very careful.
Also, if the type you mention in new
expression, is user-defined type, then the default constructor would be called currentByteLength
number of times, to construct the objects!
For built-in types, there is no constructor!