对齐的malloc和标准malloc之间的区别? [英] Difference between aligned malloc and standard malloc?
问题描述
我是malloc和对齐malloc的新手.我知道如何使用它们.但是,我不确切知道在那种情况下我们应该使用对齐的malloc而不是标准的malloc.你能给我解释一下吗?
I'm new to malloc and aligned malloc. I know how to use them. However, I don't know exactly in which case we should use aligned malloc instead of standard malloc. Can you explain it to me please?
推荐答案
glibc
文档相当清楚地表明您应该在哪里使用aligned_alloc
:
在GNU系统中,由
malloc
或realloc
返回的块的地址始终为8的倍数(在64位系统上为16).如果您需要一个块的地址是其高2的幂的倍数,请使用aligned_alloc
或posix_memalign
.
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
将为任何标准类型返回适当对齐的内存块,但是在某些情况下,您可能需要或需要更严格的对齐方式.
The C standard already guarantees that malloc
will return a suitably aligned memory block for any of the standard types but there may be situations in which you want or need stricter alignment.
作为一个例子,我似乎想起了SSE2(SIMD)指令需要将其数据对齐在16个字节的边界上,因此即使在malloc
仅保证与8对齐的系统上,您也可以使用aligned_alloc
为您提供数据字节边界.
As one example, I seem to recall that SSE2 (SIMD) instructions need their data aligned on 16-byte boundaries so you could use aligned_alloc
to give you that even on systems where malloc
only guarantees alignment to an 8-byte boundary.
这篇关于对齐的malloc和标准malloc之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!