Unexplained behavior on bool variable

Guilherme Müller gmuller@mastersolve.com.br
Fri Jul 4 20:33:00 GMT 2008


Hi, my name is Guilherme Müller, and today something very weird happened 
to me. I couldn't realize why this is happening, so, I decided to show 
you to check if this is a compiler weird behavior or something. This 
never happened to me later, and I guess it would be very hard to 
reproduce, so I don't know if it will be helpful sending an email to 
this group. Anyway,

Here is the code: (I don't need to pass the size into this method cause 
is a defined fixed size AND it's tested before calling it, so don't call 
me "bad coder" =D)

 User * User::FromNetworkSerializedData(const unsigned char * data)
      {

        User * user = new User();
       
        int readptr = 1;
      
          .
          .
          .

        unsigned char AccessFlags = 0;
       
        bool logic = ((data[readptr] & 28) > 0);  // this line as is 
return always false or something else, even if the comparison is true
   
          // Anyway, if I do this now:
          // cout << logic << endl;

          // or even THIS:
          // logic = logic;

          // the comparison works and everything goes fine!
          // weird, isn't it?

        if (logic)
          {
            AccessFlags = (data[readptr] & 28);
            user->AndLogic = false;
          }
        else
          {
            AccessFlags = ((data[readptr] >> 3) & 28);
            user->AndLogic = true;
          }

          .
          .
          .
  
        return user;
    }

This is not a call for help, just check if this could be a memory 
corruption or something. As I said, it could be very hard to 
reproduce... Good Luck.

Guilherme Müller






More information about the Gcc-help mailing list