What is difference between new and malloc?

Answer

Click on the link to your right for the answer.

Answer

both malloc and new functions are used for dynamic memory allocations and the basic difference is: malloc requires a special "typecasting" when it allocates memory for eg. if the pointer used is the char pointer then after the processor allocates memory then this allocated memory needs to be typecasted to char pointer i.e (char*).but new does not requires any typecasting. Also, free is the keyword used to free the memory while using malloc and delete the keyword to free memory while using new, otherwise this will lead the memory leak.

Answer

Besides the basic syntactical difference: malloc is a C function which will allocate the amount of memory you ask and thats it. new is a C++ operator which will allocate memory AND call the constructor of the class for which's object memory is being allocated.

Similarily, free is a C function which will free up the memory allocated. but delete is a C++ operator which will free up the allocated memory AND call the destructor of the object.

Answer

malloc in a function and new is an operator, and its a good programming practice to use an operator instead of functions, because function itself requires some time to be executed whenever that particular function is called. so the main difference is that we use operators instead of malloc because of the TIME CONSTRAINT.

Answer

1.malloc requires the type casting during decleration , where as new dosn't needed the type casting during decleration 2. when ever we use new for allocating memory along with this it calls the constructor of the class for which object memory is allocated 3. in case of malloc free is the word used to clear the memory, where as delete is the format used in case of new to free the memory after usage 4. malloc is function, where as new is operator..so the time required for execution is less in case of new (it being a operator) as compared to malloc(it being a function)

Answer

1. malloc is a function call, while new is an operator. This difference is syntactic; behind the scenes, they both perform pretty much the same work to allocate the memory, and operator new also invokes any required constructors. There is a commonplace urban myth that operators are somehow faster in your code than functions; this is not correct, as any operator (except for mathematical operations that correspond directly to a single machine-code instruction) invocation amounts to a function call in any case. 2. malloc can fail, and returns a NULL pointer if memory is exhausted. Operator new never returns a NULL pointer, but indicates failure by throwing an exception instead. There is also a nothrow() version of operator new, which does return NULL on failure.

 

Improve Answer View existing comments for "What is difference between new and malloc?" Watch Question

First answer by Ranger22. Last edit by Dave.K. Contributor trust: 7 [recommend contributor]. Question popularity: 74 [recommend question]


Research your answer:

Can you answer other questions about programming?

Answers.com > Wiki Answers > Categories > Technology > Computers > Computer Programming > What is difference between new and malloc?

Our contributors said this page should be displayed for the questions below. (Where do these come from)
If any of these are not a genuine rephrasing of the question, please help out and edit these alternates.
How does malloc work?  Example program for malloc?  Difference between new and malloc?  Difference betwen malloc and new in C?  What is the difference new and malloc?  Difference between malloc and new operator?  What is the difference between malloc and new?