How to allocate and free aligned memory in C

后端 未结 3 1165
醉酒成梦
醉酒成梦 2021-01-30 07:15

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 po

3条回答
  •  星月不相逢
    2021-01-30 08:06

    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.

提交回复
热议问题