- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
如果我定义一个具有特定对齐要求的简单类型,该类型的 std::vector<t>
难道不应该为每个元素 遵守对齐吗?
考虑下面的例子
typedef std::array<double,3> alignas(32) avx_point;
std::vector<avx_point> x(10);
assert(!(std::ptrdiff_t(&(x[0]))&31) && // assert that x[0] is 32-byte aligned
!(std::ptrdiff_t(&(x[1]))&31)); // assert that x[1] is 32-byte aligned
我发现 clang 3.2(带或不带 -stdlib=libc++
)悄悄地(没有任何警告)违反了对齐要求,而 gcc 4.8.0 发出警告,它忽略了 std::vector
(英特尔编译器)的模板参数上的属性太愚蠢了,无法理解 alignas
,但如果我改用 __declspec(align(32))
,它的行为就像 clang)。两者都创建触发断言的代码。
那么,这是正确的行为还是 clang(和 icpc)的错误以及 gcc 的问题?
编辑回答评论中提出的一个问题:如果我定义
typedef typename std::aligned_storage<sizeof (avx_point),
alignof(avx_point)>::type avx_storage;
我明白了
sizeof (avx_storage) == 32;
alignof(avx_storage) == 32;
但是 std::vector<avx_storage>
仍然无法为 clang 和 gcc 对齐第一个元素(因此也无法对齐所有其他元素)(这次没有警告)。因此,这些实现显然存在两个问题:首先,std::allocator<type>
甚至忽略了第一个元素的任何对齐要求(非法?),其次,没有应用填充来确保后续元素的对齐。
–––––––––––
编辑 有一个关于如何获得适合 SSE/AVX 操作对齐的内存的 related, more practical question。相反,我想知道 std::vector<>
(或 std::allocator<>
)是否不应该遵守 C++ 标准(截至 2011 年)的 alignas
。其他问题的答案都不是这个问题的合适答案。
最佳答案
first, that std::allocator ignores any alignment requirements even for the first element (illegal?)
我远不是分配器方面的专家,但不幸的是,在我看来,这是合法行为。更准确地说,分配器可能会忽略请求的对齐。事实上,[allocator.requirements],17.6.3.5/6 状态:
If the alignment associated with a specific over-aligned type is not supported by an allocator, instantiation of the allocator for that type may fail. The allocator also may silently ignore the requested alignment.
您可以编写自己的分配器来为您提供对齐的内存。我以前在工作中这样做过,但不幸的是,出于版权原因,我不能透露代码 :-( 我只能说,这是显而易见的事情:它基于 _aligned_malloc
和 _aligned_free
(这是 Microsoft 的扩展)。或者您可以在 Google 上搜索“对齐的分配器”,然后会出现一些选项,其中之一是
https://gist.github.com/donny-dont/1471329
我强调,我不是这个对齐分配器的作者,而且我从未使用过它。
更新
上面的对齐分配器适用于 Visual Studio/Windows,但它可以用作在其他平台上实现对齐分配器的基础。您可以使用 posix memalign
函数族或 C11 函数 aligned_alloc
。
参见 this发布。
关于c++ - std::vector 应该尊重 alignof(value_type) 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57597173/
我在玩简单的例子时注意到,alignof(struct_name) 始终是其结构的最大 alignof 的 alignof成分。情况总是(或几乎总是)如此吗? 最佳答案 Is the alignof
假设 T 和 const T 是两种大小相同且对齐方式相同的类型似乎是合理的,但在考虑了一些实际系统之后,似乎它们可能会有所不同。 让我解释一下: 假设您的系统具有两种类型的内存:RAM 和闪存(只读
我想强制特定的堆分配返回一个 64 字节对齐的地址,因为这是缓存行边界。我以为我可以这样做 int *p = new alignas(64) int; 但我的编译器似乎都没有给 p 一个 64 的倍数
我正在尝试设计一个标记指针类,其中指针的最低有效位可用作标志,但前提是它实际可用,即指向类型的对齐大于 1。 下面的工作,除了最后的问题,alignof() 不能应用于不完整的类型。 #include
在 C++11 中,我可以在没有对象的非静态数据成员上使用 alignof 吗? 如果我有一个类,例如 class A { int num; unsigned char letter; }
sizeof(char) 始终为 1,在我看来,类型的对齐要求永远不会大于其大小。引用即将发布的 C++11 标准 (3.11): An aligment is an implementation-d
我刚刚偶然发现了 std::alignment_of类型特征,以及它即将成为 friend 的 std::alignment_of_v。它们似乎经过专门设计,相当于对 alignof 的简单调用。 ,
sizeof 和 alignof 有什么区别? #include #define SIZEOF_ALIGNOF(T) std::cout<< sizeof(T) << '/' << alignof(
我现在使用共享内存。 我无法理解 alignof 和 alignas。 cppreference不清楚: alignof 返回“对齐”,但什么是“对齐”?为要对齐的下一个 block 添加的字节数?填
我无法理解 alignas 的目的是什么和 alignof关键字是,我不太确定我完全理解对齐是什么。 据我了解,如果内存地址可被 n 整除,则它与 n 字节对齐,也就是说,可以通过一次计算“n”个字节
我刚刚了解了 alignof 和 alignas C++ 关键字,但我想不出任何开发人员想要使用这些关键字的实际案例。 有人知道这些关键字的任何实际用例吗? 最佳答案 alignas 说明符的一个常见
我正在尝试运行 alignof 运算符的示例。 #include struct Empty {}; struct Foo { int f2; float f1; cha
以下代码(msvc 2012,32 位): #include int main() { double d; std::cout<<_alignof(double)<
是 alignof(T*)对于所有可能的类型 T 是相同的值?怎么样sizeof(T*) ? 请根据标准允许/指定的内容而不是不同编译器的当前情况进行回答。 最佳答案 该标准对指针的大小和对齐没有太多
如果我定义一个具有特定对齐要求的简单类型,该类型的 std::vector 难道不应该为每个元素 遵守对齐吗? 考虑下面的例子 typedef std::array alignas(32) avx_p
我正在阅读一些关于 C++ 中对齐的内容,但我不确定为什么仅包含 char 数组成员的类的对齐不是数组的大小,但事实证明始终为 1。例如 #include struct Foo{char m_[16
如果我定义一个具有特定对齐要求的简单类型,该类型的 std::vector 难道不应该为每个元素 遵守对齐吗? 考虑下面的例子 typedef std::array alignas(32) avx_p
对于任何对象类型 T,是否总是 sizeof(T) 至少与 alignof(T) 一样大? 直觉上似乎是这样,因为即使您调整对象的对齐方式,例如: struct small { char c; }
alignof(N) == sizeof(N) 其中 N 是否为整数类型? 我要求同时使用 C 和 C++,希望这不是问题。 最佳答案 它是特定于机器的。在 32 位机器上,alignof(int64
我是一名优秀的程序员,十分优秀!