What is the difference between creating an object,
using 'new' and using 'malloc'?
Answers were Sorted based on User's Feedback
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 |
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 |
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 |
Mention the default functions in C++, how would you detect that error has occurred inside the constructor and destructor.
Write a program to display the following output using a single cout statement Maths=90 Physics=77 Chemistry = 69
How does stack look in function calls? When does stack overflow? What can you do to remedy it?
How many times will this loop execute? Explain your answer.
what do you mean by exception handling in C++?
What's the value of the expression 5["abxdef"]?
What are the major differences between C and C++?
What is Copy Constructor?
What is bool in C++
Can we provide one default constructor for our class?
what is a pragma in C++?
Explain encapsulation in C++.