Skip to main content
- Calling Constructors: new calls constructors, while malloc() does not. In fact, primitive data types (char, int, float.. etc) can also be initialized with new. Ex:
int *n = new int(10); - operator vs function: new is an operator, while malloc() is a function.
- return type: new returns exact data type, while malloc() returns void *.
- Failure Condition: On failure, malloc() returns NULL were as new throws bad_alloc exception.
- Memory: In the case of new, memory is allocated from free store whereas in malloc() memory allocation is done from the heap.
- Overriding: We are allowed to override the new operator whereas we can not override the malloc() function legally.
- Size: Required size of memory is calculated by the compiler for new, whereas we have to manually calculate the size for malloc().
- Buffer Size: malloc() allows to change the size of the buffer using realloc() while new doesn’t
Comments
Post a Comment