answersLogoWhite

0


Best Answer

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

โˆ™ 2009-01-17 00:33:00
This answer is:
๐Ÿ™
0
๐Ÿคจ
0
๐Ÿ˜ฎ
0
User Avatar
Study guides
๐Ÿ““
See all Study Guides
โœ๏ธ
Create a Study Guide

Add your answer:

Earn +20 pts
Q: What is the difference between malloc and new other than syntax?
Write your answer...
Submit
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 Mexicans and other Latinos?

The difference between Mexicans and other Latinos is that they are from different countries.


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.


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 cut and past?

The difference between 'cut and paste' and what other method?


What is the difference between java and java script?

Java is a programming language and JavaScript is a scripting language. A scripting language is a lightweight programming language. They are both independent of each other, they just share a similar name and similar syntax.


What is the difference between didactics and applied linguistics?

Didactics is a teaching method for giving one's knowledge to the next, applied linguistics on the other hand is applying the rules of language and syntax in the real world.


What is the difference between main memory and other memory location?

2 difference between main memory and other memory locations


What is the difference between PCs other computers OSs?

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


Difference between plasma to the other state of matter?

the difference between plasma and the other states of matter is that,plasma is an ionized gas.


What is the difference betwween Knowledge and Skill in the field of language acquisition?

In the field of language acquisition, the difference between 'knowledge' and 'skill' is fundamentally attached to the difference between 'knowing about' and 'actually doing.' With 'knowledge,' one knows about grammar, syntax, intonation, and other important features of language. With 'skill,' one is able to use one's knowledge of those important features of language in order actually to speak the language.


What is malloc function?

The malloc function is a function used in C/C++ and various other high-level programming languges to add memory to the heap, it is essentially a pointer int* X = malloc(sizeof(int)); Where the parameter to malloc is the number of bytes you want to make room for. Because we are using an integral variable we want to make as much room as an integer takes.


What is the difference between the three genres of literature?

difference between novel and other genres of literature


What is the difference between human birth and other primate?

There is no much difference between the human birth and other primates. The process is basically the same.


What is the difference between the pigs and the other animals?

The difference is: your motha. Final answer.


Difference between HTML and xHTML 1.0?

HTML uses SGML-like syntax. XHTML 1.0 uses XML syntax. HTML uses HTML DOM, XHTML uses XML DOM. There are small differences between those. Other than that, they're identical. They use same tags, attributes, have same semantics. XHTML 1.0 specification is very short and refers to HTML 4 specification for all important matters.


Difference between epithelium and connective tissue other than location in the body?

= Difference between epithelium and connective tissue other than location in the body? =


What is the difference between an elastomer and plastics?

There isn't too much of a difference between elastomers and plastics. The only real difference between them is how far you can stretch them. Other than that they are really similar.


What is the difference between a simple subject and a compound subject?

the difference between the two of them are they both subject which compliment each other.


Is there a difference between coffee cake and streussell?

No, there is no much difference between the coffee cake and streussell other than the recipes.


What is the difference between a Civil War and a normal war?

The difference is a civil war is fought between people of the same country and other wars involve other countries.


What is the difference between heft and density?

No difference one is just fancier than the other


What is the difference between monerans and other one-celled organisms?

the difference is that they are different colors