SSE和C ++容器 [英] SSE and C++ containers

查看:117
本文介绍了SSE和C ++容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码是否有明显的原因会导致错误?

Is there an obvious reason why the following code segfaults ?

#include <vector>
#include <emmintrin.h>

struct point {
    __m128i v;

  point() {
    v = _mm_setr_epi32(0, 0, 0, 0);
  }
};

int main(int argc, char *argv[])
{
  std::vector<point> a(3);
}

感谢

编辑:我在linux / i686上使用g ++ 4.5.0,我可能不知道我在这里做什么,但是因为甚至以下segfaults

I'm using g++ 4.5.0 on linux/i686, I might not know what I'm doing here, but since even the following segfaults

int main(int argc, char *argv[])
{
  point *p = new point();
}

我真的认为它必须和对齐问题。

I really think it must be and alignment issue.

推荐答案

可能出错的明显的事情是如果 v 未正确对齐。

The obvious thing that could have gone wrong would be if v wasn't aligned properly.

但是它是由向量动态分配的,因此不会发生堆栈不对齐问题。

But it's allocated dynamically by vector, so it isn't subject to stack misalignment issues.

但是,由于 phooji 正确地指出,一个模板或原型值被传递到 std :: vector 构造函数,它将被复制到向量的所有元素。这是 std :: vector :: vector 的这个参数,将被放置在堆栈上,可能未对齐。

However, as phooji correctly points out, a "template" or "prototype" value is passed to the std::vector constructor which will be copied to all the elements of the vector. It's this parameter of std::vector::vector that will be placed on the stack and may be misaligned.

一些编译器有一个编译指令用于控制函数内的堆栈对齐(基本上,编译器浪费一些额外的空间,以使所有本地对象正确对齐)。

Some compilers have a pragma for controlling stack alignment within a function (basically, the compiler wastes some extra space as needed to get all locals properly aligned).

Microsoft文档, Visual C ++ 2010应为SSE类型自动设置8字节堆栈对齐从Visual C ++ 2003开始

According to the Microsoft documentation, Visual C++ 2010 should set up 8 byte stack alignment automatically for SSE types and has done so since Visual C++ 2003

对于gcc我不知道。

在C ++ 0x下,返回未对齐的存储是一种严重的不合规。 [basic.stc.dynamic.allocation] 说(草案n3225的措辞):

Under C++0x, for new point() to return unaligned storage is a serious non-compliance. [basic.stc.dynamic.allocation] says (wording from draft n3225):


分配函数尝试分配所请求的存储量。如果它成功,它将
返回存储块的开始的地址,其字节长度应至少与所请求大小的
一样大。对从
分配函数返回时分配的存储空间的内容没有约束。通过连续调用
分配函数分配的存储的顺序,连续性和初始值是未指定的。返回的指针应适当对齐,以便它可以将
转换为具有基本对齐要求(3.11)的任何完整对象类型的指针,然后使用
访问分配的存储中的对象或数组(直到存储通过调用
a对应的释放函数被显式释放)。

The allocation function attempts to allocate the requested amount of storage. If it is successful, it shall return the address of the start of a block of storage whose length in bytes shall be at least as large as the requested size. There are no constraints on the contents of the allocated storage on return from the allocation function. The order, contiguity, and initial value of storage allocated by successive calls to an allocation function are unspecified. The pointer returned shall be suitably aligned so that it can be converted to a pointer of any complete object type with a fundamental alignment requirement (3.11) and then used to access the object or array in the storage allocated (until the storage is explicitly deallocated by a call to a corresponding deallocation function).

[basic.align] 说:


此外,请求运行时分配动态存储b $ b请求的对齐不能被视为分配失败。

Additionally, a request for runtime allocation of dynamic storage for which the requested alignment cannot be honored shall be treated as an allocation failure.

您可以尝试更新版本的gcc,固定?

Can you try a newer version of gcc where this might be fixed?

这篇关于SSE和C ++容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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