Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

new vs HeapAlloc()

Posted on 1998-10-13
4
Medium Priority
?
1,181 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

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

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

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…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…

618 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