Possible Duplicate:
What is the difference between new/delete and malloc/free?
I am a noob in c++, want to know whether
memblock = (char *)malloc( currentByteLength);
is equivalent to
memblock = new char[currentByteLength]
in c++?
Possible Duplicate:
What is the difference between new/delete and malloc/free?
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!
Almost. I mean, both allocate data on the heap, and when dealing with primitive types such as char or int, it's about the same. There's an important difference, however, when dealing with objects. new invokes the object's constructor, while malloc doesn't. That's because malloc is a C function, and doesn't know anything about classes.
In general, new is the prefered way to dynamically allocate memory in C++, since it's typesafe and easier to use. Nothing prevents you from using malloc, but don't mix those up: do not call delete on memory allocated with malloc, or free on memory allocated with new. Bad things will happen otherwise. And do not use delete on an array created with new[], or delete[] on an object created with new (yes, I know that's confusing, that's why you should use a vector instead).
If the memory block is allocated with malloc(), you have to free it by calling free(). If it was allocated by means of new[], you have to delete it by calling delete []. Using the wrong method of disposition is an error.
Also, new and malloc behave very differently when you allocate arrays of objects.
Also, they report out-of-memory condition differently - new throws an exception, malloc returns a zero.
The new keyword allocates memory in the free-store. The malloc function allocates memory in the heap.
Many implementations use the heap to implement the free-store, but they don't have to be related at all. They can, and often do, have completely different managers.
As far as your code should be concerned, the memory blocks allocated by your particular examples could be considered equivalent. This is a pretty corner case though and you still need to make sure you deallocate the block in the correct place (free-store vs. heap). It's better if you just never consider them the same thing at all.
Here's a good tutorial on memory in C++: http://www.gotw.ca/gotw/009.htm
not exactly, malloc() is C function and new is C++ operator, also if your case their a both allocate buffer but if malloc() fails then is return NULL. but if new fails is throw an exception.
for more info read this session in C++ FAQ
Short answer: Yes, that is how you allocate memory dynamically in C++.
Long answer: No, they are different beasts. The most obvious difference is that memory that is malloc()'ed must be free()'ed, while memory that is new'ed must be deleted.
new calls the default constuctor of the type, is type-safe, throws an exception on error, and can be overridden.
In C++, you should always be using new over malloc(), unless you need to interface with code written in C.
Yes:
malloc / free vs. operator new[] / operator delete[])malloc returns 0, operator new[] throws an exception.This is only in your particular case though. There are even more differences when:
malloc -- this would illustrate how operator new is more intuitive for allocating objects instead of a blob)I don't think this is a great question... what are you really asking? Difference in terms of what? No, you cannot interchange them blindly.
It is roughly equivalent (exception/null and constructor aside) in the case that char is 1 byte in size, otherwise for the first one you should multiply by the data type size.
memblock = (char *)malloc( currentByteLength * sizeof(char) );
Note that
memblock = new DATATYPE[length]
is equivalent to
memblock = (DATATYPE*) malloc(length * sizeof(DATATYPE));
If and only if there is no constructor, and if and only if datatype is not a pointer itselfs.
Free and delete are equivalent only if there's no destructor.
If there's no constructor/destructor, you can call new/malloc delete/free interchangably and even with mixing.
new and delete create the memory for a new object calling the constructor/destructor (if available), while malloc and free just allocate the memory (without calling constructor/destructor).
The main differences (specifically referring to char) are:
FYI: should always put a sizeof check for the malloc:
memblock = (char *)malloc( sizeof(char) * currentByteLength);