上交所和C集装箱

前端之家收集整理的这篇文章主要介绍了上交所和C集装箱前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
是否有明显的原因为什么以下代码segfaults?
#include <vector>
#include <emmintrin.h>

struct point {
    __m128i v;

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

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

谢谢

编辑:我在linux / i686上使用g 4.5.0,可能不知道我在做什么,但是即使是以下的segfaults

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

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

解决方法

可能会出错的明显的事情是,如果v没有正确对齐.

但是它由向量动态分配,所以它不会受到堆栈不对齐问题的影响.

但是,正如phooji正确指出的那样,一个“模板”或“原型”值被传递给std :: vector构造函数,它将被复制到向量的所有元素.这是std :: vector :: vector的这个参数,它将被放置在堆栈上,并且可能不对齐.

一些编译器有一个用于在一个函数内控制堆栈对齐的编译指示(基本上,编译器根据需要浪费一些额外的空间,使所有本地人正确对齐).

根据Microsoft文档,Visual C++ 2010 should set up 8 byte stack alignment automatically for SSE typeshas done so since Visual C++ 2003

对于gcc我不知道.

在C 0x下,对于新点()返回未对齐的存储是严重违规. [basic.stc.dynamic.allocation]说(草案n3225的措辞):

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]说:

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,这可能在哪里修复?

原文链接:https://www.f2er.com/c/110588.html

猜你在找的C&C++相关文章