o4-mini-2025-04-16· OpenAI
LMArena Human Preference 140K
How do I allocate memory in C with a guaranteed alignment?
- User
How do I allocate memory in C with a guaranteed alignment?
- Assistant
Here are the most common ways to get aligned memory in C, across different platforms and standards:
- C11 (and later) “aligned_alloc”
- POSIX “posix_memalign”
- Windows “_aligned_malloc” / “_aligned_free”
- Manual (portable) over-allocation trick
—---
1) C11 “aligned_alloc”
#include <stdlib.h> size_t alignment = 64; // must be power of two size_t size = 1024; // any size void *ptr = aligned_alloc(alignment, size); if (!ptr) { // allocation failed } // … use ptr … free(ptr); // use free()Notes:
alignmentmust dividesize(i.e.size % alignment == 0).- Defined in
<stdlib.h>under C11.
—---
2) POSIX “posix_memalign”
#include <stdlib.h> void *ptr = NULL; size_t alignment = 64; // any valid alignment: power of two ≥ sizeof(void*) size_t size = 1024; int err = posix_memalign(&ptr, alignment, size); if (err != 0) { // handle error (err == ENOMEM or EINVAL) } // … use ptr … free(ptr); // use free()Notes:
- Does not require
size % alignment == 0. - Returns zero on success or an error code.
- Portable to all POSIX‐compliant systems (Linux, macOS, BSD, etc.).
—---
3) Windows “_aligned_malloc”
#include <malloc.h> size_t alignment = 64; size_t size = 1024; void *ptr = _aligned_malloc(size, alignment); if (!ptr) { // allocation failed } // … use ptr … _aligned_free(ptr);Notes:
- Defined in
<malloc.h>. - Pair
_aligned_mallocwith_aligned_free.
—---
4) Portable Manual Trick
When none of the above are available, you can over‐allocate and adjust:
#include <stdlib.h> #include <stdint.h> void* aligned_malloc_portable(size_t size, size_t alignment) { // allocation: original + alignment - 1 + space to store original ptr uintptr_t needed = size + alignment - 1 + sizeof(void*); void *raw = malloc(needed); if (!raw) return NULL; // align the pointer uintptr_t raw_addr = (uintptr_t)raw + sizeof(void*); uintptr_t offset = raw_addr % alignment; uintptr_t adjust = (offset == 0 ? 0 : alignment - offset); void *aligned_ptr = (void*)(raw_addr + adjust); // store the original pointer just before aligned_ptr ((void**)aligned_ptr)[-1] = raw; return aligned_ptr; } void aligned_free_portable(void *ptr) { if (ptr) { // retrieve original pointer and free it void *raw = ((void**)ptr)[-1]; free(raw); } }Usage:
void *p = aligned_malloc_portable(1024, 64); // … use p … aligned_free_portable(p);—---
Quick Comparison Table
Method Standard Requirement Free with aligned_alloc C11 size % alignment == 0 free() posix_memalign POSIX.1-2001 alignment ≥ sizeof(void*) free() _aligned_malloc/_aligned_free Microsoft CRT — _aligned_free Manual overallocation portable C89/C99 — free(raw) —---
Tips & Gotchas
- Always choose an alignment that’s a power of two.
- On UNIX, prefer
posix_memalignfor flexibility (size needn’t be multiple of alignment). - On Windows, match
_aligned_mallocwith_aligned_free. - Check for allocation failures!
That’s it—pick the method supported by your platform and C standard, and you get rock-solid alignment. 🚀
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.