Solved

memory question

Posted on 1998-05-11
2
273 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
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 goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

752 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