Solved

new vs HeapAlloc()

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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

This article shows a few slightly more advanced techniques for Windows 7 gadget programming, including how to save and restore user settings for your gadget and how to populate the "details" panel that is displayed in the Windows 7 gadget gallery.  …
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
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…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

728 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