answersLogoWhite

0

When new has an object, space for the object is not only allocated but the object's constructor is called. And similarly when delete as an object, the object's destructor is called before the memory is released. If malloc and free are used, the destructor and constructor do not get called respectively and obviously, this simply won't do in C++ except in certain very rare situations where classes are present without any specific destructor/constructors. Operator new automatically calculates the size of the object that it constructs. Conversely, with malloc(), the programmer has to specify explicitly the number of bytes that have to be allocated. In addition, malloc() returns void *, which has to be explicitly cast to the desired type. This is both tedious and dangerous. Operator new returns a pointer to the desired type, so no explicit typecast is required. new and delete automatically construct and destroy objects. malloc() and free(), on the other hand, merely allocate and deallocate raw memory from the heap. delete can be overloaded, free cannot. delete invokes the destructor of the object to be deallocated, free does not do this. The difference could be qouted as - 1. Operator new constructs an object (calls constructor of object), malloc does not. 2. Operator new is an operator, malloc is a function. 3. Operator new can be overloaded, malloc cannot be overloaded. 4. Operator new throws an exception if there is not enough memory, malloc returns a NULL. 5. Operator new[] requires to specify the number of objects to allocate, malloc requires to specify the total number of bytes to allocate. 6. malloc() returns void *, which has to be explicitly cast to the desired type but new returns the proper type. 7. Operator new/new[] must be matched with operator delete/delete[] to deallocate memory, malloc() must be matched with free() to deallocate memory. 8. The new/delete couple does not have a realloc alternative that is available when malloc/free pair is used. realloc is used to resize the length of an array or a memory block dynamically.

User Avatar

Wiki User

16y ago

What else can I help you with?

Related Questions

What is the difference between malloc and new in terms of throwing exceptions?

malloc will return a 0 if memory is unable to be allocated. new on the other hand will either throw an exception or also return 0, depending on the compiler and the compiler settings.


What is the difference between Typedef and Reference in C plus plus?

A typedef is a compiler macro. A reference is a pointer, usually implemented with transparent syntax. They have no relationship between each other.


What is different between malloc and free?

malloc reserves memory for your use, free removes the reservation done by malloc and makes the memory available for other applications.Note: both of them is a special case of realloc:malloc (n) = realloc (NULL, n)free (p) = realloc (p, 0)


What is the difference between syntax and semantics in programming languages?

Syntax in programming languages refers to the rules and structure that dictate how code is written, such as correct use of punctuation and keywords. Semantics, on the other hand, refers to the meaning and interpretation of the code, including how the instructions are executed and the logic behind them. In essence, syntax is about the form of the code, while semantics is about its function and behavior.


What is the difference between grammar and syntax?

Grammar refers to the rules and structure of a language, including things like sentence structure, punctuation, and parts of speech. Syntax, on the other hand, specifically refers to the arrangement of words in a sentence to create meaning. In simpler terms, grammar is the overall system of rules in a language, while syntax focuses on how those rules are applied in forming sentences.


What is difference between linear syntax and relational algebra?

They are not alternative solutions so that we can compare them. Relational database (which is based on relational algebra) demands (atleast the founder of relational database Codd suggests) that the query language follow linear syntax. The linear syntax languages don't rely up on newline characters as terminators of expressions or statements -- instead they rely on other tokens such as semicolon or comma and so on.


What is the difference between grammar and syntax in the context of language structure?

Grammar refers to the rules and principles that govern the structure of sentences and how words are used in a language. Syntax, on the other hand, specifically deals with the arrangement of words in a sentence to create meaning and convey information. In essence, grammar is the broader set of rules governing language, while syntax focuses on the order and structure of words within sentences.


Difference between isql plus and sql plus?

Basically in isql we use browser and we can also use mouse here. But in sql we use terminal where we cannot use the mouse. Commands syntax are different from each other.


What is the difference between morphology and syntax?

Morphology is the study of the structure and formation of words, including prefixes, suffixes, and root words. Syntax, on the other hand, deals with the arrangement of words to create well-formed sentences, including word order, sentence structure, and grammar rules that govern how words come together to form meaningful phrases and sentences.


What is the difference between delete and free?

Are you talking about freeing dynamically allocated memory in C/C++? free() is a function that you use to release dynamically (i.e. at run-time) created memory in C, using malloc() or alloc() or such other functions. In the same way, delete() is a function that is used in C++ to release memory created at run-time using the function new(). (Note that you can still use malloc and other C functions in your C++ code, but it is not considered a good programming habit. Moreover, new() is easier to use and more flexible, once you get the hang of it. If this is not what you had in mind, then I do not know if this will be of any help to you. addition: -new is constructor of which delete is destructor so use in pairs always.. similarly use malloc with free.. extra note: - no type cast required for new , whereas malloc, free may require it. - new returns exception whereas malloc returns NULL when memory issue.


What is the difference between PCs other computers OSs?

What is the difference between a car and other means of transport? Got it?


What is the difference between cooperative and other business organization?

Check