为什么动态分配的内存总是16个字节对齐? [英] Why is dynamically allocated memory always 16 bytes aligned?

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

问题描述

我写了一个简单的例子:

I wrote a simple example:

#include <iostream>

int main() {
    void* byte1 = ::operator new(1);
    void* byte2 = ::operator new(1);
    void* byte3 = malloc(1);
    std::cout << "byte1: " << byte1 << std::endl;
    std::cout << "byte2: " << byte2 << std::endl;
    std::cout << "byte3: " << byte3 << std::endl;
    return 0;
}

运行示例,我得到以下结果:

Running the example, I get the following results:

字节1:0x1f53e70

byte1: 0x1f53e70

byte2:0x1f53e90

byte2: 0x1f53e90

byte3:0x1f53eb0

byte3: 0x1f53eb0

每次我分配一个内存字节时,它总是对齐16个字节.为什么会这样?

Each time I allocate a single byte of memory, it's always 16 bytes aligned. Why does this happen?

我在GCC 5.4.0和GCC 7.4.0上测试了此代码,并得到了相同的结果.

I tested this code on GCC 5.4.0 as well as GCC 7.4.0, and got the same results.

推荐答案

为什么会这样?

Why does this happen?

因为该标准是这样说的.更具体地说,它说动态分配 1 至少与最大基本 2 对齐(可能具有更严格的对齐)对齐.有一个预定义的宏(自C ++ 17起)只是为了告诉您确切的保证对齐方式是什么:__STDCPP_DEFAULT_NEW_ALIGNMENT__.为什么在您的示例中可能是16 ...这是语言实现的一种选择,受目标硬件体系结构所允许的限制.

Because the standard says so. More specifically, it says that the dynamic allocations1 are aligned to at least the maximum fundamental2 alignment (it may have stricter alignment). There is a pre-defined macro (since C++17) just for the purpose of telling you exactly what this guaranteed alignment is: __STDCPP_DEFAULT_NEW_ALIGNMENT__. Why this might be 16 in your example... that is a choice of the language implementation, restricted by what is allowed by the target hardware architecture.

这是(是)必要的设计,因为考虑到(没有)没有办法将有关所需对齐方式的信息传递给分配函数(直到C ++ 17引入了aligned-new语法来分配对齐"的内存.

This is (was) a necessary design, considering that there is (was) no way to pass information about the needed alignment to the allocation function (until C++17 which introduced aligned-new syntax for the purpose of allocating "over-aligned" memory).

malloc对要在内存中创建的对象类型一无所知.有人可能会认为new在理论上可以推断出对齐方式,因为它被赋予了类型...但是,如果您想将该内存用于具有更严格对齐方式的其他对象,例如std::vector的实现,该怎么办?并且一旦知道了运算符的新API:void* operator new ( std::size_t count ),您就会发现类型或其对齐方式不是可能影响分配对齐方式的参数.

malloc doesn't know anything about the types of objects that you intend to create into the memory. One might think that new could in theory deduce the alignment since it is given a type... but what if you wanted to reuse that memory for other objects with stricter alignment, like for example in implementation of std::vector? And once you know the API of the operator new: void* operator new ( std::size_t count ), you can see that the type or its alignment are not an argument that could affect the alignment of the allocation.

1 由默认分配器或malloc函数族制成.

1 Made by the default allocator, or malloc family of functions.

2 最大基本对齐方式是alignof(std::max_align_t).没有基本类型(算术类型,指针)比这更严格的对齐方式.

2 The maximum fundamental alignment is alignof(std::max_align_t). No fundamental type (arithmetic types, pointers) has stricter alignment than this.

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

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