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

Write a program in c++ to print the numbers from n to n2 except 5 and its multiples

2273


Explain "const" reference arguments in function?

841


What is a singleton class c++?

786


What is the difference between encapsulation and polymorphism?

861


Can a list of string be stored within a two dimensional array?

787


Declare a pointer to a function that takes a char pointer as argument and returns a void pointer.

789


What are the 3 principles of oop?

853


Write a program to show polymorphism in C++?

862


Discussion on error handling of C++ .

875


Define private, protected and public access control.

870


What is namespace std; and what is consists of?

965


Explain stack unwinding.

861


What is the difference between multiple and multilevel inheritance in c++?

910


Is it possible to get the source code back from binary file?

1040


Write a Program for dynamically intialize a 2 dimentional array. Eg:5x20, accept strings and check for vowels and display the no.finally free the space allocated .

937