Bug: initializing volatile int corrupts other data

Bug: initializing volatile int corrupts other data

attila.afra的头像

It seems that I've found a bug in the Intel C++ Compiler (2013.1.117, 2013.0.079, didn't try older versions) on 64-bit Linux. The following code prints random garbage instead of a constant (1234) if compiled with any kind of optimization:

// Compile: icpc volatilebug.cpp
#include <stdio.h>
struct Vec
{
    int x;
    int y;
    Vec() {}
    explicit Vec(int x) : x(x), y(x) {}
};
struct P
{
    void init(const Vec& v)
    {
        va = v;
        vb = v;
        c = vb.x; // no error with: c = va.x
    }
    Vec va;
    Vec vb;
    int c;
};
struct Q
{
    void init(const P& k)
    {
        p = k;
        w = 7777;
        printf("%d", p.vb.x); // prints garbage instead of 1234!
    }
    P p;
    volatile int w; // no error without volatile!
};
int main()
{
    P p;
    p.init(Vec(1234));
    Q q;
    q.init(p);
    return 0;
}

If I remove the volatile, p.vb.x will contain the correct value. It's also fine if I compile it without optimization or with GCC. Could anyone confirm this problem? Did I miss something or is it really a bug in the optimizer? Thanks!

Regards,
Attila

附件尺寸
下载 volatilebug.cpp591 字节
5 帖子 / 0 new
最新文章
如需更全面地了解编译器优化,请参阅优化注意事项
attila.afra的头像

The bug also appears with version 2011.9.300 on Windows for both 32-bit and 64-bit compilations.

Georg Zitzlsberger (Intel)的头像

Hello Attila,

I think that this is a bug, too. Indeed an interesting one as I was not able to shorten it further. Hence I passed it on as is to engineering as a defect ticket (DPD200240148). I'll keep you in the loop.

Thank you for finding!

Best regards,

Georg Zitzlsberger

attila.afra的头像

Thanks!

Georg Zitzlsberger (Intel)的头像

Hello Attila,

this will be fixed with Intel(R) Composer XE 2013 Update 4 (available in a few months from now).

Best regards,

Georg Zitzlsberger

登陆并发表评论。