Solved

memory question

Posted on 1998-05-11
2
249 Views
Last Modified: 2010-04-10
What is the exact difference between

char *pBuf = new char[10];

and

char *pBuf = (char*)malloc(10);

both will allocate space on the heap?!
0
Comment
Question by:dib
2 Comments
 
LVL 1

Accepted Solution

by:
Blondie050798 earned 50 total points
ID: 1175610
Specifically for MS VC++, new eventually calls malloc, so architecturally there is no difference. However, VC++ and I imagine all other compilers create clever debug versions of new to trap memory reuse, leaks etc. You have also given a very simple example, new comes into its own as a flexible allocator designed for allocating classes not as a char allocator. (nb I'd probably use an automatic or static array anyway!)
0
 
LVL 5

Expert Comment

by:yonat
ID: 1175611
One difference is that "new" can be re-defined by the programmer. Another, is that it is type safe (no need for the casting, no need to calculate the size if you want to allocate an array of amything bu char). But the most important difference is when you need to allocate OBJECTS, and not just plain chars: new will call the object's constructor, while malloc will not.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

929 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

8 Experts available now in Live!

Get 1:1 Help Now