This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

c++/2860: incorrectly initialized auto variable



>Number:         2860
>Category:       c++
>Synopsis:       incorrectly initialized auto variable
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    unassigned
>State:          open
>Class:          wrong-code
>Submitter-Id:   net
>Arrival-Date:   Thu May 17 17:46:00 PDT 2001
>Closed-Date:
>Last-Modified:
>Originator:     Florian Kainz
>Release:        gcc version 3.0 20010514 (prerelease)
>Organization:
>Environment:
% g++ -v
Reading specs from /usr/local/lib/gcc-lib/i686-pc-linux-gnu/3.0/specs
Configured with: ../gcc-20010514/configure --enable-threads --prefix=/usr/local --enable-languages=c,c++
Thread model: posix
gcc version 3.0 20010514 (prerelease)
>Description:
-O3 appears to (incorrectly) optimize away part
of the initialization code for an auto variable.
>How-To-Repeat:
Compile the program below with

    g++ -O3 xxx.C

and run the resulting executable;
it should print:

    a1: 1, 2, 3, 4
    a2: 1.5, 2.5, 3.5, 4.5

Instead, it prints:

    a1: 1, 2, 3, 4
    a2: 1.4013e-45, 2.8026e-45, 3.5, 4.5

//
// xxx.C
//

#include <iostream>

template <class T>
struct Vec2
{
    T x, y;
    Vec2 ()                     {}
    Vec2 (T a, T b)             {x = a; y = b;}
    Vec2 (const Vec2 &v)        {x = v.x; y = v.y;}
};

typedef Vec2 <int>    V2i;
typedef Vec2 <float>  V2f;

template <class T>
struct Box
{
    T min;
    T max;
    Box(const T& minT, const T& maxT) {min = minT; max = maxT;}
};

typedef Box <V2i> Box2i;
typedef Box <V2f> Box2f;

int
main ()
{
    Box2i a1 (V2i (1, 2),     V2i (3, 4));
    Box2f a2 (V2f (1.5, 2.5), V2f (3.5, 4.5));

    std::cout << "a1: " <<
                 a1.min.x << ", " <<
                 a1.min.y << ", " <<
                 a1.max.x << ", " <<
                 a1.max.y << std::endl;

    std::cout << "a2: " <<
                 a2.min.x << ", " <<
                 a2.min.y << ", " <<
                 a2.max.x << ", " <<
                 a2.max.y << std::endl;
}
>Fix:

>Release-Note:
>Audit-Trail:
>Unformatted:


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]