Professional Documents
Culture Documents
Malloc Vs New
Malloc Vs New
Malloc Vs New
===========================
Both malloc() and new, are used to allocate memory dynamically in the heap. the
functionality of these are seems to be the same but they differ in the following
ways.
1) malloc() is a C library function that can be used in c++ while "new" is a
operator or keyword which is available in C++ only.
2) "new" calls the contructor of the class when we use to allocate memory
dynamically while malloc() does not.
3) the "new" operator returns the specific type of pointer as it is declared hence,
there is not required to type-cast to assign the pointer. while malloc() return
void pointer hence, typecasting is required to assign the pointer.
example -
#include <iostream>
using namespace std;
class student {
public:
student() {
cout << endl << "student class constructor";
}
};
int main() {
return EXIT_SUCCESS;
}