上交所和C集装箱

上交所和C集装箱,第1张

概述是否有明显的原因为什么以下代码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::vec 是否有明显的原因为什么以下代码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 types和has 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,这可能在哪里修复?

总结

以上是内存溢出为你收集整理的上交所和C集装箱全部内容,希望文章能够帮你解决上交所和C集装箱所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1257751.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-07
下一篇 2022-06-07

发表评论

登录后才能评论

评论列表(0条)

保存