?
Solved

new vs HeapAlloc()

Posted on 1998-10-13
4
Medium Priority
?
1,173 Views
Last Modified: 2013-12-03
What's more efficient, new or HeapAlloc()?

(First, sorry for my english...is very bad.)

Of course, for objects we must use 'new' because HeapAlloc() only reserve memory and don't call for de constructors and destructors (delete).

But, what is more efficient in reserver small or medium  amounts of memory?

I'm programming in BC++ 5.01.
0
Comment
Question by:tui
[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
  • 3
4 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 400 total points
ID: 1415221
You are best of using new for everything.  It has slightly higher overhead to allocate, but this is not significant.  In addition it will allow the system to run faster as a whole, which may make up for its additional overhead.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1415222
Your C++ program will allocate several heaps from Window's memory.  New will in term allocate from these heaps when your program needs memory.  This is a good scheme because no matter how many times your program allocates or destroys memory with new, windows does not see it.  That is, windows does not heed to change its heap.  Thus window's heap stays simple with a small number of large blocks.  This helps windows to load and free programs quickly.  

There are other advatanges to using new as well.  You don't have to worry about mistakenly allocating an object with a constructor using HeapAlloc()  You don't have to worrk about mistakenly calling delete on memory allocated with HeapAlloc().  

In addition, in VC if you use new, you can use the debug heap features to do some very important test for memory errors.  You can't do this with HeapAlloc().

If you still have doubts.  Do a test.  Create a program that Allocates 1000 blocks and destroys them again. Do this process about a 1000 times, in the progrma.  i.e. you will have about 1,000,000 allocations and deletions.)  Try it with new one item and with HeapAlloc() the another time.  I don't think you will find a measurable difference in time.
0
 

Author Comment

by:tui
ID: 1415223
Thank you for your answer nietod.

Another little question, can I make a new heap in a  process or thread and use new for reserve memory in that new heap? I want to make a new class that manage it's own memory and protect the 'principal heap' from mistakes.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1415224
Yes.  You can overload the new and delete operators for that class so that it alocates out of its private heap.  (If you will have arrays of these objects, you will also need to overload new [] and delete [].)  T
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

This article describes how to add a user-defined command button to the Windows 7 Explorer toolbar.  In the previous article (http://www.experts-exchange.com/A_2172.html), we saw how to put the Delete button back there where it belongs.  "Delete" is …
zlib is a free compression library (a DLL) on which the popular gzip utility is built.  In this article, we'll see how to use the zlib functions to compress and decompress data in memory; that is, without needing to use a temporary file.  We'll be c…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

770 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