How to manage memory alignments and generic pointer arithmetics in a portable way in C?

后端 未结 6 476
暖寄归人
暖寄归人 2021-01-13 11:22

I have to implement an optimized version of malloc/realloc/free (tailored for my particular application). At the moment the code runs on a particular platform, but I would l

6条回答
  •  时光说笑
    2021-01-13 11:45

    Alignment features are only handled in the new C standard, C11. It has keywords _Alignof, _Alignas and a function aligned_alloc. Theses features are not very difficult to emulate with most modern compilers (as indicated in other answers), so I'd suggest you write yourself small macros or wrappers that you'd use depending on __STDC_VERSION__.

提交回复
热议问题