gpt4 book ai didi

malloc - 什么是对齐的内存分配?

转载 作者:行者123 更新时间:2023-12-03 15:04:43 28 4
gpt4 key购买 nike

我也想知道glibc malloc()是否这样做。

最佳答案

Alignment要求指定可以将哪些地址偏移量分配给什么类型。这完全取决于实现,但通常基于字长。例如,某些32位体系结构要求所有int变量以4的倍数开头。在某些架构上,对齐要求是绝对的。在其他(例如x86)上,轻视它们只会导致性能下降。

需要malloc返回适合任何对齐要求的地址。换句话说,可以将返回的地址分配给任何类型的指针。根据C99§7.20.3(内存管理功能):

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).

关于malloc - 什么是对齐的内存分配?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3994035/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com