C Programming
The Difference Between
Similarities Between

What is the difference between malloc and new other than syntax?


Top Answer
User Avatar
Wiki User
2009-01-17 00:33:00
2009-01-17 00:33:00

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.


Related Questions

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.

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)

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

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.

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

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 a car and other means of transport? Got it?

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.

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

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.

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.

2 difference between main memory and other memory locations

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

difference between novel and other genres of literature

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.

The difference is: your motha. Final answer.

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

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.

other = different another= additional

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? =

WHATS THE DIFFERENCE BETWEEN ANTS & FLIES The difference between ants and flies is that one can fly and the other one crawls. Also one is mostly in the air, while the other one is mostly on land.

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

The difference between 144 and 40 is 104.In other words, 144 - 40 = 104

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

Copyright ยฉ 2020 Multiply Media, LLC. All Rights Reserved. The material on this site can not be reproduced, distributed, transmitted, cached or otherwise used, except with prior written permission of Multiply.