SEGFAULT在-O3模式? [英] SEGFAULT in -O3 mode?

查看:182
本文介绍了SEGFAULT在-O3模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将我的问题总结到以下短程序中。



它会在-O3模式下导致SEGFAULT(-O2工作正常)。
根据 gdb 它发生在 * f = 0 行。

  #include< iostream> 

void func1(int s,int t)
{
char * buffer = new char [s + t * sizeof(float)];
if(!buffer)
{
std :: cout< new failed\ n;
return;
}
float * f =(float *)(buffer + s);
for(int i = 0; i {
* f = 0;
// std :: cout<< i<< std :: endl; //如果取消注释这行一切都会正常工作
++ f;
}
delete [] buffer;
std :: cout<< done\\\
;
}

int main()
{
int s = 31,t = 12423138;
std :: cout<< s<< < t < std :: endl;
func1(s,t);
return 0;
}

请让我知道,我做错了什么?

解决方案

SEGFAULT的来源并不完全违反严格别名规则,因为即使使用-fno-strict-aliasing标志,问题仍然存在。 / p>

这是访问未对齐的内存,但不是那么简单。作为现代处理器,通常允许未对齐的存储器访问,并且甚至没有太多的开销现在。我做了一些基准测试,没有观察到在我的Intel(R)Xeon(R)CPU E5-2680 v2 @ 2.80GHz上的algined vs unaligned read的很大区别。此外,还有一些



我的问题是 -O3 模式启用 -ftree-vectorize 标志,因此我的循环被矢量化(正如我可以看到使用 -ftree-vectorizer-verbose flag)。对于使用向量化指令的未对齐内存访问,没有支持(AFAIU),因此存在运行时异常。



a href =http://www.ibm.com/developerworks/library/pa-dalign/ =nofollow>这篇文章帮助我了解了很多理论,虽然似乎今天未对齐内存访问不是有害的,虽然仍然棘手


I summarized my problem to the following short program.

It causes SEGFAULT in -O3 mode only (-O2 works fine). According to gdb it happens at *f = 0 line.

#include <iostream>

void func1(int s, int t)
{
        char* buffer = new char[s + t*sizeof(float)];
        if (!buffer)
        {
            std::cout << "new failed\n";
            return;
        }
        float* f = (float*)(buffer + s);
        for (int i = 0; i < t; ++i)
        {
            *f = 0;
            //std::cout << i << std::endl; // if uncomment this line everything will work fine
            ++f;
        }
        delete [] buffer;
        std::cout << "done\n";
}

int main()
{
        int s = 31, t = 12423138;
        std::cout << s << " " << t << std::endl;
        func1(s, t);
        return 0;
}

Please let me know, what am I doing wrong?

解决方案

The source of SEGFAULT was not solely in violation of the strict aliasing rule, as the problem persisted even with -fno-strict-aliasing flag.

It was indeed accessing unaligned memory, but not as simple as that. As modern processors, generally allow unaligned memory access and there is even not much of an overhead nowadays. I've done some benchmarking and didn't observe a big difference in algined vs unaligned read on my Intel(R) Xeon(R) CPU E5-2680 v2 @ 2.80GHz. Also there are some very similar (and more or less recent) results in the web.

My problem was that -O3 mode enables -ftree-vectorize flag, therefore my for cycle was vectorized (as I could see using -ftree-vectorizer-verbose flag). And (AFAIU) there is no support (yet?) for unaligned memory access using vectorized instructions, so there was a runtime exception.

This article helped me out a lot in understanding theory, though it seems that today unaligned memory access is not as harmful as it was, though still tricky

这篇关于SEGFAULT在-O3模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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