Solved

malloc and new???

Posted on 1998-04-08
3
223 Views
Last Modified: 2010-04-10
Hi,
Can anyone pls. tell me what's the precise difference between malloc in C and new in C++. And also between free in C and delete in C++.

thanx.

KM
0
Comment
Question by:kmahmood
  • 2
3 Comments
 
LVL 1

Expert Comment

by:slinky
ID: 1184140
How precise do you want?

with malloc you get a pointer to the number of bytes you asked for in the call, it's pretty much up to you how you use them

new returns a pointer to a new instance of the type of object you asked for, so you get type information as well as not having to work out how many bytes you need (no sizeof)


0
 
LVL 22

Accepted Solution

by:
nietod earned 0 total points
ID: 1184141
answer coming.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1184142
malloc() allocates memory but does not initialize it in any way.  The memory will contain seemingly random informaiton left over from the last time it was used.

The new operator tries to initialize the memory.  It uses malloc() (ussually) to allocate the memory.  Then it looks to see if the object has a constructor.  If so, it calls the constructor to iniitalize the memory.  This is vital in many cases.  If you malloc memory for an object that must be initialized and the object does not get initialized it will usually cause serious problems.  New makes sure this does not happen

New has three safety features that malloc does not.  First it initializs the memory if needed.  Second, as slinky said, it insures that the right amount of memory is allocated.  If you malloc() too little space for an object it will usually cause a crash.  new prevents this.  Finally new returns a pointer of the right type.  This means you don't have to typecase the pointer returned as you do with malloc.  This prevents mistakes that occur when you typecast to the wrong type by mistake.

There is no reason to use malloc in C++.  If you want "raw" memory, use new to create an array of characters.  This is equivilant to using malloc.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question