Intel® C++ Compiler
Community support and assistance for creating C++ code that runs on platforms based on Intel® processors.

Bug: initializing volatile int corrupts other data

attila_afra
Beginner
359 Views

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:

[cpp]

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


[/cpp]
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

0 Kudos
4 Replies
attila_afra
Beginner
359 Views

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

0 Kudos
Georg_Z_Intel
Employee
359 Views
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
0 Kudos
attila_afra
Beginner
359 Views

Thanks!

0 Kudos
Georg_Z_Intel
Employee
359 Views
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
0 Kudos
Reply