Aligned memory management?

后端 未结 7 1873
清歌不尽
清歌不尽 2020-12-13 05:00

I have a few related questions about managing aligned memory blocks. Cross-platform answers would be ideal. However, as I\'m pretty sure a cross-platform solution does not

7条回答
  •  天命终不由人
    2020-12-13 05:31

    1. I'm not aware of any way of requesting malloc return memory with stricter alignment than usual. As for "usual" on Linux, from man posix_memalign (which you can use instead of malloc() to get more strictly aligned memory if you like):

      GNU libc malloc() always returns 8-byte aligned memory addresses, so these routines are only needed if you require larger alignment values.

    2. You must free() memory using the same pointer returned by malloc(), posix_memalign() or realloc().

    3. Use realloc() as usual, including sufficient extra space so if a new address is returned that isn't already aligned you can memmove() it slightly to align it. Nasty, but best I can think of.

提交回复
热议问题