Bug: initializing volatile int corrupts other data

Bug: initializing volatile int corrupts other data

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

AnexoTamanho
Download volatilebug.cpp591 bytes
5 posts / novo 0
Último post
Para obter mais informações sobre otimizações de compiladores, consulte Aviso sobre otimizações.

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

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

Thanks!

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

Deixar um comentário

Faça login para adicionar um comentário. Não é membro? Inscreva-se hoje mesmo!