Archived

This topic is now archived and is closed to further replies.

kuphryn

Windows Memory Allocation :: C++

Recommended Posts

Hi. C++ offers the new operator to allocate a section of memory via heap. C offers the general malloc set of functions to allocate a section of memory via heap. Windows offers a series of functions. I would like to know what is the best memory allocation tool to use under windows. Again, I am a C++ programmer. I believe Windows has these memory related functions. GlobalAlloc() GlobaFree() HeapAlloc() HeapFree() LocalAlloc() LocalFree () I would like to like under a C++ Windows program, what are the most appropriate memory allocation tool to use? Currently, I use C++ new operator to develop Windows winsock programs. However, I see programmers use GlobalAlloc almost all the time for Windows and Windows network programs. What is the advantage, if any, of GlobalAlloc that C++ new lacks, if any? Thanks, Kuphryn

Share this post


Link to post
Share on other sites
kuphryn, although I am not a extremely experienced programmer I dont''t think it really matters what you use to allocate memory for your program. Using the c/c++ memory allocation functions, however will make your program more portable than if you were to use windows memory allocation functions.

How about them apples?

Share this post


Link to post
Share on other sites
In C++, use the new and delete operators (as well as new[] and delete[] operators) as they call the constructors and destructors. The default implementations of new/delete actually call malloc and free (in VC++ anyway).

The GlobalAlloc/GlobalFree and LocalAlloc/LocalFree are the exact same under win32 (no different between local and "far" pointers in win32, these functions are only there for backwards compatibility with win16).

I know that using the HeapAlloc function, with the HEAP_NO_SERIALIZE flag will speed up allocations in a single-threaded program... so if you''re making a single-threaded C++ app under windows, you should overwrite the new / delete operators to use HeapAlloc / HeapFree instead of malloc/free to gain a slight performance increase.

Share this post


Link to post
Share on other sites
you never know what you can dig out browsing members'' profiles!

here are some (hopefully) interesting facts regarding the various memory allocators:

VirtualAlloc: the heavylifter. use this if you need lots of memory, such as several megabytes; you''ll bypass lots of heap management code. keep in mind that allocation granulatiry is currently 64 KB, and your requests will be rounded up to that figure. works nicely with structured exception handling and VirtualProtect when you want to have a huge array of something, but are not likely to use many of its elements at any time.

HeapAlloc: suggested Win32 solution for small allocation requests. you can have multiple heaps in a single application to combat heap fragmentation and decrease lookup time for allocation requests.

GlobalAlloc: ms says you shouldn''t use it for performance reasons, however it''s still used by clipboard, dde, drag and drop, and other interprocess data exchange services.

LocalAlloc: its use is also discouraged, however some applications use it if they don''t want to deal with heap handles that HeapAlloc requires. Note that LocalAlloc and GlobalAlloc memory is generally not interchangeable; even though the docs say that there are no separate global and local heaps, that doesn''t mean you can give a LocalAlloc''d pointer to the clipboard.

new: the c++ way to do things. it throws bad_alloc on allocation failure, which is very useful if you''re using exceptions for error handling because you can forget about doing all the pAllocatedItem != 0 checks and handle bad_alloc along with other exceptions. Internally implemented on top of malloc.

malloc: the c way to do things. provides additional management code on top of HeapAlloc, but doesn''t require a heap handle as HeapAlloc does (it uses the default process heap for allocations). in msvc7 (not sure about 6), malloc can call your _set_new_handler routine (note leading underscore; it''s an ms feature, not c++ standard one), which allows you to make malloc throw exceptions. the actual usability of this feature in my view is, however, small because existing code doesn''t expect malloc to throw exceptions, and therefore will not clean up properly if it does. malloc is implemented on top of HeapAlloc. a nice feature of malloc is the ability to resize allocated blocks via realloc without manually copying data to new block, which new doesn''t support.

_DBG_MAP_ALLOC and <crtdbg.h> will give you the debug heap which provides additional error checking, such as overwrite/underwrite checks, delayed memory releases, checks for writes to freed blocks, memory leak checking, and so on. this is the most important reason for using either new or malloc over its win32 counterparts: you get free debug heap diagnostics in debug builds. win32 also provides some heap diagnostics, but it''s much less useful than msvc debug heap.

ATL_MIN_CRT will make new and malloc barebone wrappers on top of HeapAlloc.

Share this post


Link to post
Share on other sites
Okay. Thanks.

C++ new operator and C runtime memory managementfunctions malloc are two very useful memory management functions indeed. After working on Win32 projects for Windows, I ofter find developer still use GlobalAlloc and HeapAlloc.

Kuphryn

Share this post


Link to post
Share on other sites
well, some things requre GlobalAlloc-allocated memory, so naturally you''d use it for them. You would prefer HeapAlloc over new/malloc when you''re trying to avoid crt code to minimize your exe size.

Share this post


Link to post
Share on other sites