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

Answers were Sorted based on User's Feedback



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

Answer / btech

new operator returns a pointer of the correct type and
malloc() returns a void*

New calls the object’s constructor and malloc does not.

Any object created with new must be freed using delete and
where as malloc() and free() allocates and deallocates
memory

new operator can be overloaded by a class, where as
malloc() can't be overloaded.

Is This Answer Correct ?    9 Yes 0 No

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

Answer / 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

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

Answer / guna

malloc
1) can't initialize the memory
2)type casting is required
3)malloc is a function.
4)can't be overloaded.
new
1) memory can be initialized
2)no type casting for operator
3)new is an operator
4)can be overloaded.

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More C++ Interview Questions

What Is A Default Constructor in C++ ?

0 Answers   Amazon,


In C++ what is a vtable and how does it work?

0 Answers   Agilent,


What does malloc return in C and C++?

0 Answers   Alter,


Identify the errors in the following program. #include <iostream> using namespace std; void main() { int i=5; while(i) { switch(i) { default: case 4: case 5: break; case 1: continue; case 2: case 3: break; } i-; } }

1 Answers  


Explain why C++ is not purely Object Oriented Language

0 Answers   Aspire,






What is a virtual function in C++?

0 Answers   C DAC,


Write a C++ Program to find Square Root of a number using sqrt() function.

1 Answers  


Execute the qsort () in c/sort() in c++ library or your own custom sort which will sort any type of data on user defined criteria.

0 Answers   Adobe,


What is an algorithm (in terms of the STL/C++ standard library)?

0 Answers   Amazon,


How will you print a list of all unique words from a string which may contain repeated words?

0 Answers   Adobe,


How to delete array of objects in C++? Proof by C++ code for proper deletion

0 Answers  


Define namespace.

1 Answers  


Categories