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

Identify the error in the following program. include<iostream> using namespace std; void main() { int num[]={1,2,3,4,5,6}; num[1]==[1]num ? cout<<"Success" : cout<<"Error"; }

1 Answers  


What does malloc return in C and C++?

0 Answers   Alter,


How can you force the compiler to not generate them?

0 Answers   Amazon,


What are the costs and benefits of using exceptions?

0 Answers   Amazon,


Explain the difference between C and C++.

0 Answers   Accenture,






What is latest update of C++ ?

0 Answers   Adobe,


C++ Public access specifier instead of Private – What is bad ?

0 Answers  


What do you by Function Overloading in C++?

0 Answers   Akamai Technologies, Infogain,


There is a base class sub, with a member function fnsub(). There are two classes super1 and super2 which are sub classes of the base class sub.if and pointer object is created of the class sub which points to any of the two classes super1 and super2, if fnsub() is called which one will be inoked?

0 Answers   Alter,


What is a COPY CONSTRUCTOR and when is it called?

0 Answers   IBS,


Write a syntax and purpose of switch statement.

0 Answers   Agilent,


What kind of problems does name mangling cause?

0 Answers   Amazon,


Categories