Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

malloc and new???

Posted on 1998-04-08
3
Medium Priority
?
227 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.
Suggested Courses

971 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