Skip to content
TheChatArchiveThe Chat Archive
o4-mini-2025-04-16· OpenAIHugging Face LMArena Human Preference 140K

How do I allocate memory in C with a guaranteed alignment?

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with o3-mini’s answer.
  1. User

    How do I allocate memory in C with a guaranteed alignment?

  2. Assistant

    Here are the most common ways to get aligned memory in C, across different platforms and standards:

    1. C11 (and later) “aligned_alloc”
    2. POSIX “posix_memalign”
    3. Windows “_aligned_malloc” / “_aligned_free”
    4. 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:

    • alignment must divide size (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_malloc with _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

    MethodStandardRequirementFree with
    aligned_allocC11size % alignment == 0free()
    posix_memalignPOSIX.1-2001alignment ≥ sizeof(void*)free()
    _aligned_malloc/_aligned_freeMicrosoft CRT—_aligned_free
    Manual overallocationportable C89/C99—free(raw)

    —---

    Tips & Gotchas

    • Always choose an alignment that’s a power of two.
    • On UNIX, prefer posix_memalign for flexibility (size needn’t be multiple of alignment).
    • On Windows, match _aligned_malloc with _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. 🚀

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.

Identical first prompt