24

How do you allocate memory that's aligned to a specific boundary in C (e.g., cache line boundary)? I'm looking for malloc/free like implementation that ideally would be as portable as possible --- at least between 32 and 64 bit architectures.

Edit to add: In other words, I'm looking for something that would behave like (the now obsolete?) memalign function, which can be freed using free.

4
  • Have you checked these two answers on SO: stackoverflow.com/questions/227897/… and stackoverflow.com/questions/1855896/…? Dec 17, 2009 at 3:01
  • Not quite what I'm looking for. I would like something that functions more like malloc, returns one value that is the aligned pointer, then have another function similar to free, that's called on that pointer. Applying a solution such as the ones you pointed requires either passing around two values, or recalculating the alignment whenever it's needed. I'm looking for something similar to memalign. Thanks for pointing those answers out though.
    – fuad
    Dec 17, 2009 at 3:32
  • 1
    There's posix_memalign() on appropriate machines - has a different interface from memalign(). Dec 17, 2009 at 3:38
  • 1
    Possible duplicate of aligned malloc() in GCC? Mar 16, 2017 at 7:22

3 Answers 3

26

Here is a solution, which encapsulates the call to malloc, allocates a bigger buffer for alignment purpose, and stores the original allocated address just before the aligned buffer for a later call to free.

// cache line
#define ALIGN 64

void *aligned_malloc(int size) {
    void *mem = malloc(size+ALIGN+sizeof(void*));
    void **ptr = (void**)((uintptr_t)(mem+ALIGN+sizeof(void*)) & ~(ALIGN-1));
    ptr[-1] = mem;
    return ptr;
}

void aligned_free(void *ptr) {
    free(((void**)ptr)[-1]);
}
7
  • 2
    with the new standards, you should consider replacing the cast to long with a cast to uintptr_t Feb 14, 2013 at 22:46
  • @Jermoe In particular, the code does not work on 64-bit Windows
    – user877329
    Jun 29, 2015 at 14:49
  • @Jerome — Any particular reason you say malloc(size+ALIGN+sizeof(void*)) and not malloc(size+ALIGN-1+sizeof(void*))? If you're aligning to an n-byte boundary, then you only ever need at most n – 1 extra bytes. Aug 21, 2015 at 5:17
  • This code is somewhat confusing in its use of the void** in the return statement.
    – einpoklum
    Oct 22, 2015 at 16:26
  • Is it okay to add +ALIGN-1 instead of +ALIGN? void aligned_malloc(int size) { void *mem = malloc(size+ALIGN-1+sizeof(void)); void ptr = (void)((long)(mem+ALIGN-1+sizeof(void*)) & ~(ALIGN-1)); ptr[-1] = mem; return ptr; }
    – Dewr
    Nov 1, 2015 at 23:54
15

Use posix_memalign/free.

int posix_memalign(void **memptr, size_t alignment, size_t size); 

void* ptr;
int rc = posix_memalign(&ptr, alignment, size);
...
free(ptr)

posix_memalign is a standard replacement for memalign which, as you mention is obsolete.

4

What compiler are you using? If you're on MSVC, you can try _aligned_malloc() and _aligned_free().

3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.