How to align pointer

前端 未结 6 2375
说谎
说谎 2020-12-01 04:29

How do I align a pointer to a 16 byte boundary?

I found this code, not sure if its correct

char* p= malloc(1024);

if ((((unsigned long) p) % 16) !=          


        
6条回答
  •  北荒
    北荒 (楼主)
    2020-12-01 05:09

    In glibc library malloc, realloc always returns 8 bytes aligned. If you want to allocate memory with some alignment which is a higher power 2 then you can use memalign and posix_memalign. Read http://www.gnu.org/s/hello/manual/libc/Aligned-Memory-Blocks.html

提交回复
热议问题