Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Allocating aligned data (memalign) in Visual C++

I have a small prototype program written in C++ running under Linux using the memalign function to allocate regions aligned to boundaries like pages. Is there an equivalent function in Visual C++?

like image 343
J D Avatar asked Jan 10 '11 10:01

J D


People also ask

What is Memalign in C?

The memalign function allocates a block of size bytes whose address is a multiple of boundary . The boundary must be a power of two! The function memalign works by allocating a somewhat larger block, and then returning an address within the block that is on the specified boundary.

What is aligned Alloc?

The aligned_alloc function allocates space for an object whose alignment is specified by alignment, whose size is specified by size, and whose value is indeterminate.

Does malloc guarantee alignment?

The only standard rule is that the address returned by malloc will be suitably aligned to store any kind of variable. What exactly that means is platform-specific (since alignment requirements vary from platform to platform).

Is malloc 16 byte aligned?

The GNU documentation states that malloc is aligned to 16 byte multiples on 64 bit systems.


1 Answers

You're looking for _aligned_malloc. Note that memory must be deallocated using _aligned_free, and that alignment is restricted to powers of 2.

If you need more flexibility, it's not too hard to handroll your own solution which mallocs alignment + bytes memory chunks to start with.

like image 75
Eugene Talagrand Avatar answered Sep 27 '22 15:09

Eugene Talagrand