From: sudhir.kadkade@googlemail.com   
      
   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    
   # define INCLUDED_CSTDDEF   
   # endif   
      
   # ifndef INCLUDED_IOSTREAM   
   # include    
   # 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] = i;   
    }   
   }   
      
   template   
   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   
   inline   
   BitArray::BitArray()   
    : BitArrayBase(sizeInWords)   
   {   
   }   
      
   template   
   inline   
   unsigned int   
   BitArray::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   
      
      
   --   
    [ See http://www.gotw.ca/resources/clcm.htm for info about ]   
    [ comp.lang.c++.moderated. First time posters: Do this! ]   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   
|