Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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
";
            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
";
}

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
323 views
Welcome To Ask or Share your Answers For Others

1 Answer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...