什么是对齐的内存分配? [英] What is aligned memory allocation?

查看:64
本文介绍了什么是对齐的内存分配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我还想知道glibc malloc()是否这样做.

I also want to know whether glibc malloc() does this.

推荐答案

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

Alignment requirements specify what address offsets can be assigned to what types. This is completely implementation-dependent, but is generally based on word size. For instance, some 32-bit architectures require all int variables start on a multiple of four. On some architectures, alignment requirements are absolute. On others (e.g. x86) flouting them only comes with a performance penalty.

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

malloc is required to return an address suitable for any alignment requirement. In other words, the returned address can be assigned to a pointer of any type. From C99 §7.20.3 (Memory management functions):

如果分配则返回指针 成功地进行适当调整,以便 它可以分配给任何指针 对象的类型,然后用于访问 这样的对象或这样的数组 分配空间中的对象(直到 该空间已明确释放).

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

这篇关于什么是对齐的内存分配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆