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

后端 未结 6 511
暖寄归人
暖寄归人 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:53

    C says malloc returns a pointer to memory aligned for any purpose. There is no portable way in C to achieve that with C features. This has the consequence that malloc is a function that if written in C cannot be written in a portable way.

    (C99, 7.20.3p1) "The pointer returned if the allocation succeeds is suitably aligned so that it may be assigned to a pointer to any type of object and then used to access such an object or an array of such objects in the space allocated (until the space is explicitly deallocated)."

提交回复
热议问题