对齐的 malloc 和标准 malloc 之间的区别?
Difference between aligned malloc and standard malloc?
我是 malloc 和对齐 malloc 的新手。我知道如何使用它们。但是,我不知道在什么情况下我们应该使用对齐的 malloc 而不是标准的 malloc。你能给我解释一下吗?
glibc
documentation 清楚地说明了应该在哪里使用 aligned_alloc
:
The address of a block returned by malloc
or realloc
in GNU systems is always a multiple of eight (or sixteen on 64-bit systems). If you need a block whose address is a multiple of a higher power of two than that, use aligned_alloc
or posix_memalign
.
C 标准已经保证 malloc
将为任何标准类型 return 适当对齐的内存块,但在某些情况下您可能想要或需要更严格的对齐方式。
作为一个例子,我似乎记得 SSE2 (SIMD) 指令需要它们的数据在 16 字节边界上对齐,因此即使在 malloc
的系统上,您也可以使用 aligned_alloc
为您提供数据仅保证与 8 字节边界对齐。
我是 malloc 和对齐 malloc 的新手。我知道如何使用它们。但是,我不知道在什么情况下我们应该使用对齐的 malloc 而不是标准的 malloc。你能给我解释一下吗?
glibc
documentation 清楚地说明了应该在哪里使用 aligned_alloc
:
The address of a block returned by
malloc
orrealloc
in GNU systems is always a multiple of eight (or sixteen on 64-bit systems). If you need a block whose address is a multiple of a higher power of two than that, usealigned_alloc
orposix_memalign
.
C 标准已经保证 malloc
将为任何标准类型 return 适当对齐的内存块,但在某些情况下您可能想要或需要更严格的对齐方式。
作为一个例子,我似乎记得 SSE2 (SIMD) 指令需要它们的数据在 16 字节边界上对齐,因此即使在 malloc
的系统上,您也可以使用 aligned_alloc
为您提供数据仅保证与 8 字节边界对齐。