Solved

new vs HeapAlloc()

Posted on 1998-10-13
4
1,120 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
  • 3
4 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 100 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

This article shows how to make a Windows 7 gadget that extends its U/I with a flyout panel -- a window that pops out next to the gadget.  The example gadget shows several additional techniques:  How to automatically resize a gadget or flyout panel t…
As more and more people are shifting to the latest .Net frameworks, the windows presentation framework is gaining importance by the day. Many people are now turning to WPF controls to provide a rich user experience. I have been using WPF controls fo…
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

757 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

20 Experts available now in Live!

Get 1:1 Help Now