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++?
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.
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.
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).
The GNU documentation states that malloc is aligned to 16 byte multiples on 64 bit systems.
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 malloc
s alignment + bytes
memory chunks to start with.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With