Solved

malloc and new???

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now