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

Discuss the role of C++ shorthands.

0 Answers   Adobe,


How to reverse a string in C++

0 Answers  


Explain the difference between C and C++.

0 Answers   Accenture,


Name the operators that cannot be overloaded.

1 Answers   Wipro,


What Is Polymorphism in C++ ?

2 Answers   IBS, Impetus, ITC Indian Tobacco Company, Motorola,


Define type casting in C++.

0 Answers   Amdocs,


Describe the different styles of function prototypes in C++.

0 Answers   Adobe,


What is the 4 difference between delete[] and delete?

0 Answers   Alter,


How does stack look in function calls? Write a recursive function call, how will the stack look like?

0 Answers   Adobe,


Write a C++ Program to Find whether given Number is Odd or Even.

1 Answers  


Can we call C++ OOPS? and Why

0 Answers  


What Are The Differences Between A C++ Struct And C++ Class?

2 Answers   Wipro,


Categories