What alignment uses HeapAlloc

I am developing a general purpose library that uses Win32 HeapAlloc

MSDN does not mention alignment guarantees for Win32 HeapAlloc, but I really need to know what alignment it uses, so I can avoid overfilling.

On my machine (vista, x86) all distributions are aligned with 8 bytes. Is this also true for other platforms?

+7
c ++ c alignment
source share
3 answers

The HeapAlloc function does not indicate the guarantee of alignment on the MSDN page, but I am inclined to think that it should have the same GlobalAlloc guarantees, which guaranteed to return 8-byte memory memory (although it relies on undocumented functions for this is evil); in the end, he explicitly said that Global / LocalAlloc is just wrappers around HeapAlloc (although they can drop the first n bytes to get consistent memory, but I think this is very unlikely).

If you really want to be sure, just use GlobalAlloc or even VirtualAlloc, whose granularity is the granularity of the page, which is usually 4 KB (IIRC), but in this case you will waste a lot of memory for small allocations.

By the way, if you use the new C ++ operator, you are guaranteed to correctly align the memory for the type you specify: this may be the way to go.

+2
source share

Surprisingly, Google reveals evidence that HeapAlloc not always compatible with SSE:

HeapAlloc () has all objects that are always oriented by 8 bytes, regardless of their size (but not for alignment of 16 bytes for SSE).

A post from mid-2008, suggesting that the latest Windows XP is suffering from this error.

See also http://support.microsoft.com/kb/286470 :

Windows heap administrators (all versions) have always guaranteed that heap allocations have a starting address that is 8-byte aligned (on 64-bit platforms, alignment is 16 bytes).

+3
source share

The alignment will be such that the return address can be transferred to a pointer of any type. Otherwise, you will not be able to use memory in your application.

+1
source share

All Articles