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

Difference between Call by pointer and by reference.

0 Answers   Adobe,


How to run C++ program in cmd

0 Answers  


Write a C++ Program to Find Sum and Average of three numbers.

1 Answers  


Without using third variable write a code to swap two numbers.

0 Answers   Accenture,


What are the advantages/disadvantages of using #define?

0 Answers   Amazon,






When must you use a constructor initializer list?

0 Answers   Amazon,


What are "pure virtual" functions?

0 Answers   Adobe, Alter, iNautix,


What is conversion constructor in C++

0 Answers   TCS,


Difference between function overloading and function overriding.

0 Answers   Alter,


Implement a 2D bit-matrix representing monochrome pixels which will have only OFF/ON values and will take on an average only one bit of memory for each stored bit. How to perform various operations on it?

0 Answers   Adobe,


What is partial specialization or template specialization?

0 Answers   Amazon,


What Is A Conversion Constructor C++ ?

0 Answers   Amazon,


Categories