291b1641   
   From: ivnicula@this.is.invalid   
      
   On Dec 15, 5:56 pm, 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?   
   >   
   > Regards,   
   > Sudhir   
   >   
      
   > # ifndef INCLUDED_CSTDDEF   
   > # include    
   > # define INCLUDED_CSTDDEF   
   > # endif   
   >   
   > # ifndef INCLUDED_IOSTREAM   
   > # include    
   > # define INCLUDED_IOSTREAM   
   > # endif   
   >   
      
   no need for these, std headers are already guarded and you should do   
   the same   
   for yours. trivia: when was this actually needed?   
      
   > class BitArrayBase   
   > {   
   > protected:   
   > BitArrayBase(const int numberOfDataWords);   
   >   
      
   arguments taken by const value in the declaration are confusing   
   because compiler   
   ignores const here in the declaration;   
   to mark intention you can use them in the function definition, they   
   are not   
   ignored there.   
      
   > public:   
   > unsigned int d_data[sizeOfInitialArray]; // extended as needed by the   
   derived class   
   >   
   > };   
   >   
      
   an array of known bound size cannot be extended, for example the array   
   above type is   
   "array of 1 unsigned int" and that type cannot be changed.   
      
   > BitArrayBase::BitArrayBase(const int numberOfDataWords)   
   > {   
   > for (int i = 0; i < numberOfDataWords; ++i)   
   > {   
   > d_data[i] = i;   
   > }   
   >   
   > }   
      
   d_data[i] is identical to *((d_data)+(i)) per [expr.sub/1] and if we   
   focus for   
   a second on the addition part, if 'i' is bigger than array's size it   
   renders UB   
   per [expr.add/5].   
   it's also UB (with regard to your code) if 'i' equals to array's size   
   because of   
   dereferencing [expr.unary.op]/1 a pointer to no object.   
      
   in your example code, I think 'i' is 0..7 and d_data size is 1, so for   
   any i > 0   
   it triggers UB.   
      
   bottom line use std::array (C++11) or boost::array (C++03) or write   
   your own w/o   
   triggering UB.   
      
   note: UB means undefined behavior - anything can happen including   
   working as you would   
   expect - but you cannot rely on it.   
      
   hth,   
   gil   
      
      
   --   
    [ 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)   
|