The code below does not work with -O3 with gcc 4.5 or later.

Discussion in 'C++' started by Sudhir Kadkade, Dec 15, 2012.

  1. Hi all,
    The code below worked before gcc 4.5. It only works with gcc 4.5 without optimization. Could some one please explain what is wrong, and what optimzation step in gcc fails?

    Regards,
    Sudhir

    // bitArrayTest.cpp
    // Copyright 2004 Mentor Graphics Corporation. All Rights Reserved

    # ifndef INCLUDED_CSTDDEF
    # include <cstddef>
    # define INCLUDED_CSTDDEF
    # endif

    # ifndef INCLUDED_IOSTREAM
    # include <iostream>
    # define INCLUDED_IOSTREAM
    # endif

    #ifndef sizeOfInitialArray
    # define sizeOfInitialArray 1
    #endif

    class BitArrayBase
    {
    protected:
    BitArrayBase(const int numberOfDataWords);

    public:
    unsigned int d_data[sizeOfInitialArray]; // extended as needed by the derived class
    };

    BitArrayBase::BitArrayBase(const int numberOfDataWords)
    {
    for (int i = 0; i < numberOfDataWords; ++i)
    {
    d_data = i;
    }
    }

    template<int sizeInWords>
    class BitArray : public BitArrayBase
    {
    public:
    BitArray();
    unsigned int getDataWord(const int n) const;
    unsigned int getDataWord0() const;
    unsigned int getDataWord1() const;
    unsigned int getDataWord2() const;
    unsigned int getDataWord3() const;
    unsigned int getDataWord4() const;
    unsigned int getDataWord5() const;
    unsigned int getDataWord6() const;
    unsigned int getDataWord7() const;

    public:
    unsigned int d_dataExtension[sizeInWords - 1];
    };

    template<int sizeInWords>
    inline
    BitArray<sizeInWords>::BitArray()
    : BitArrayBase(sizeInWords)
    {
    }

    template<int sizeInWords>
    inline
    unsigned int
    BitArray<sizeInWords>::getDataWord(const int n) const
    {
    return d_data[n];
    }

    bool
    check(const BitArray<8> & bitArray)
    {
    for (int i = 0; i < 8; ++i)
    {
    if (i != bitArray.getDataWord(i)) return false;
    }
    return true;
    }

    void
    dump(const BitArray<8> & bitArray)
    {
    using std::cout; using std::endl; using std::hex;
    cout << "\t" << '[' << 0 << "] " << hex << bitArray.d_data[0] << endl;
    cout << "\t" << '[' << 1 << "] " << hex << bitArray.d_data[1] << endl;
    cout << "\t" << '[' << 2 << "] " << hex << bitArray.d_data[2] << endl;
    cout << "\t" << '[' << 3 << "] " << hex << bitArray.d_data[3] << endl;
    cout << "\t" << '[' << 4 << "] " << hex << bitArray.d_data[4] << endl;
    cout << "\t" << '[' << 5 << "] " << hex << bitArray.d_data[5] << endl;
    cout << "\t" << '[' << 6 << "] " << hex << bitArray.d_data[6] << endl;
    cout << "\t" << '[' << 7 << "] " << hex << bitArray.d_data[7] << endl;
    for (int i = 0; i < 8; ++i)
    {
    cout << "\t" << '[' << i << "] " << hex << bitArray.getDataWord(i) << endl;
    }
    }

    void
    runTest()
    {
    BitArray<8> bitArray;
    const bool passed = check(bitArray);
    std::cout << "Test " << (passed ? "passed." : "failed.") << std::endl;
    if (!passed) dump(bitArray);
    }

    int
    main()
    {
    runTest();
    return 0;
    }


    Here is a shell script to run it in different modes.

    #!/bin/sh

    echo "Try it without optimization"
    g++ ./bitArrayTest.cpp
    ../a.out

    echo "Try it with optimization"
    g++ -O3 ./bitArrayTest.cpp
    ../a.out

    echo "Try it with optimization, but initial allocation = 2"
    g++ -DsizeOfInitialArray=2 -O3 ./bitArrayTest.cpp
    ../a.out

    echo "Try it without optimization 32 bit"
    g++ -m32 ./bitArrayTest.cpp
    ../a.out

    echo "Try it with optimization 32 bit"
    g++ -m32 -O3 ./bitArrayTest.cpp
    ../a.out

    echo "Try it with optimization, but initial allocation = 2, 32 bit"
    g++ -DsizeOfInitialArray=2 -m32 -O3 ./bitArrayTest.cpp
    ../a.out
    Sudhir Kadkade, Dec 15, 2012
    #1
    1. Advertising

  2. Sudhir Kadkade

    Ian Collins Guest

    Sudhir Kadkade wrote:
    > Hi all,
    > The code below worked before gcc 4.5. It only works with gcc 4.5 without optimization. Could some one please explain what is wrong, and what optimzation step in gcc fails?


    Please wrap your lines!

    Define "works" and not working. Does it fail to build?

    > // bitArrayTest.cpp
    > // Copyright 2004 Mentor Graphics Corporation. All Rights Reserved
    >
    > # ifndef INCLUDED_CSTDDEF
    > # include <cstddef>
    > # define INCLUDED_CSTDDEF
    > # endif


    Why would anyone do that?

    --
    Ian Collins
    Ian Collins, Dec 15, 2012
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    5
    Views:
    358
    Nathan Addy
    Sep 17, 2005
  2. Craig Burkett
    Replies:
    2
    Views:
    621
    Craig Burkett
    Sep 11, 2003
  3. KMILO
    Replies:
    0
    Views:
    132
    KMILO
    Jan 15, 2007
  4. johnmann56
    Replies:
    2
    Views:
    109
    johnmann56
    May 24, 2004
  5. kiran
    Replies:
    12
    Views:
    1,119
    Scott Sauyet
    Dec 7, 2011
Loading...

Share This Page