answersLogoWhite

0


Best Answer

Click on the link to your right for the answer.

Answerboth malloc and new functions are used for dynamic memory allocations and the basic difference is: malloc requires a special "typecasting" when it allocates memory for eg. if the pointer used is the char pointer then after the processor allocates memory then this allocated memory needs to be typecasted to char pointer i.e (char*).but new does not requires any typecasting. Also, free is the keyword used to free the memory while using malloc and delete the keyword to free memory while using new, otherwise this will lead the memory leak. AnswerBesides the basic syntactical difference: malloc is a C function which will allocate the amount of memory you ask and that's it. new is a C++ operator which will allocate memory AND call the constructor of the class for which's object memory is being allocated.

Similarily, free is a C function which will free up the memory allocated. but delete is a C++ operator which will free up the allocated memory AND call the destructor of the object.

Answermalloc in a function and new is an operator, and its a good programming practice to use an operator instead of functions, because function itself requires some time to be executed whenever that particular function is called. so the main difference is that we use operators instead of malloc because of the TIME CONSTRAINT. Answer1.malloc requires the type casting during decleration , where as new doesn't needed the type casting during decleration 2. when ever we use new for allocating memory along with this it calls the constructor of the class for which object memory is allocated 3. in case of malloc free is the word used to clear the memory, where as delete is the format used in case of new to free the memory after usage 4. malloc is function, where as new is operator..so the time required for execution is less in case of new (it being a operator) as compared to malloc(it being a function) Answer1. malloc is a function call, while new is an operator. This difference is syntactic; Behind the Scenes, they both perform pretty much the same work to allocate the memory, and operator new also invokes any required constructors. There is a commonplace urban myth that operators are somehow faster in your code than functions; this is not correct, as any operator (except for mathematical operations that correspond directly to a single machine-code instruction) invocation amounts to a function call in any case. 2. malloc can fail, and returns a NULL pointer if memory is exhausted. Operator new never returns a NULL pointer, but indicates failure by throwing an exception instead. There is also a nothrow() version of operator new, which does return NULL on failure.
User Avatar

Wiki User

12y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

15y ago

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.

This answer is:
User Avatar

User Avatar

Wiki User

18y ago

For the dynamic memory allocation we use New and malloc. Both are used to allocate memory But the major difference between New and Malloc are. 1. New is a operator where as Malloc is a function. 2.In case of Malloc we need to specify the size of memory for our data types. But in case of New we do not need to specify the size. ex. int *a; a=(int*)malloc(sizeof(int)); int *a; a=new int; one more difference is malloc return void* by default ,we need to typecast pointer returned from malloc but in case of new there is no need to typecast ex: int *a; a=(int*)malloc(sizeof(int)); a= new int; arun

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is the difference between malloc and new other than syntax?
Write your answer...
Submit
Still have questions?
magnify glass
imp
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 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 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 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?


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.


What is the difference between cooperative and other business organization?

Check


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 between human birth and other primate?

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