Solved

malloc and new???

Posted on 1998-04-08
3
221 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

785 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