What is the difference between creating an object,
using 'new' and using 'malloc'?

Answer Posted / sundaram

?new? is used for dynamic memory allocation in C++,
?malloc()? is used for dynamic memory allocation in C.
?new? allocates memory on heap.
?malloc()? allocates memory on heap.
?new? is operator,
?malloc()? is function
?new? returns memory pointer to the correct object on
SUCCESS,
?malloc? returns pointer to void void* on success
?new? throws exception called ?bad_alloc? on FAILURE,
?malloc? returns NULL on FAILURE
?new? is 2 step process
(i) First Allocates memory for a given object
(ii) Calls corresponding destructor if required
?malloc? is one step process ie it allocating only memory.

Is This Answer Correct ?    7 Yes 2 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is abstraction example?

627


What is dynamic and static typing?

679


Consider the following C++ program

355


What is purpose of inheritance?

648


Describe public access specifiers?

591






Write a function to perform the substraction of two numbers. Eg: char N1="123", N2="478", N3=-355(N1-N2).

625


What is the difference between a type-specific template friend class and a general template friend class?

566


What is the difference between structures and unions?

588


What does namespace mean in c++?

557


What does and I oop mean in text?

631


What is latest update of C++ ?

579


What is the type of 'this' pointer? When does it get created?

552


What is destructor oops?

625


What is the difference between public, private, and protected inheritance?

552


Difference between overloaded functions and overridden functions

588