Solved

new vs HeapAlloc()

Posted on 1998-10-13
4
1,126 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

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

Title # Comments Views Activity
Method 'OpenReport' of the object Docmd failed 7 29
Problem to adjust sheet 1 86
Poor Man's .NET Framework Certification 2 47
Question to Pivot table 1 37
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 …
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
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…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

863 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

21 Experts available now in Live!

Get 1:1 Help Now