What is difference between new and malloc?

Answer Posted / shivi jain

new and delete are C++ specific features. They didn't exist in C. malloc is the old school C way to do things. Most of the time, you won't need to use it in C++.

malloc allocates uninitialized memory. The allocated memory has to be released with free.
calloc is like malloc but initializes the allocated memory with a constant (0). It needs to be freed with free.
new initializes the allocated memory by calling the constructor (if it's an object). Memory allocated with new should be released with delete (which in turn calls the destructor). It does not need you to manually specify the size you need and cast it to the appropriate type. Thus, it's more modern and less prone to errors.

Is This Answer Correct ?    7 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What do you mean by overloading?

586


What is abstraction with example?

609


Which is better struts or spring?

624


What is polymorphism what is it for and how is it used?

576


given a set based questions and 5 questions based on it next data sufficiciency questions 2 and 2/3 english sentence completion with options very easy and 2 synononmys paragraph with 10 questions 10 minutes replace =,-,*,% with -,%,+,* type questions 5 3 questions lik following itssickhere itssickthere itssickhere istsickhere which is nt alike the others very easy

2150






What is an advantage of polymorphism?

601


What is class and object in oops?

615


How to use CMutex, CSemaphore in VC++ MFC

4335


What are different types of JVM's? for example we use dalvik jvm for android then what about the remaining operating systems?

1656


If a=5, b=6, c=7, b+=a%c*2. What is the final value of b?

944


What is polymorphism programming?

607


What is the difference between a mixin and inheritance?

527


Which language is not a true object oriented programming language?

646


What is inheritance write a program to show use of inheritance?

617


class CTest { public: void someMethod() { int nCount = 0; cout << "This is some method --> " << nCount; } }; int main() { CTest *pctest; pctest->someMethod(); return 0; } It will executes the someMethod() and displays the value too. how is it possible with our creating memory for the class . i think iam not creating object for the class. Thanks in Advance... Prakash

1702