Byte Alignment for integer (or other) types in a uint8_t array

萝らか妹 提交于 2019-12-02 02:46:50

It's not true for all processors.

If I were designing the low level API, on a processor where this mattered, I'd do something like void *my_alloc(size_t size, size_t align)

If callers are manipulating character arrays, they pass an align of 1. For various small types they pass sizeof(short), sizeof(int) etc.

Then give them an allocator for the clueless, where align is MIN(roundup_to_power_of_2(size), SIZE_ON_THIS_ARCH_THATS_ALWAYS_GOOD_ENOUGH)

And no, round_up_to_power_of_2 isn't a standard function (or macro) ;-)

an easy solution is to pass the desired alignment as a second parameter to the function you write that will obtain the pointer to allocated memory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!