locked
An odd programing problem... RRS feed

  • Question

  • Hi All,

    I'm using Visual Studio 2010 with C++ and I don't understand that odd problem.

    pReducedFile = ReducedFile;

    while ((*pReducedFile != 0xCC) || i < 2000)
    {
     i = pReducedFile - ReducedFile;
     fprintf(Reduced_File, "%c", *pReducedFile);
     pReducedFile++;
     if (i > 2000)
      break;
     //if ((*pReducedFile) == 0xCC)
     if ((*pReducedFile) == 204)
      break;
    }

    while the ReducedFile is an array of chars with 2000 chars.

    My problem is  when the pointer reaches the point that it should break i.e. on the if condition: if ((*pReducedFile) == 0xCC)or the other one if ((*pReducedFile) == 204) while it has the value of CC it retunes false on the condition when it has to break.

    Why that problem occures and how can I solve it?

     

    Thanks,

    Shimon.

     

    Tuesday, October 11, 2011 10:49 AM

Answers

  • ShimonShami wrote:

    while ((*pReducedFile != 0xCC) || i < 2000)

    Try

    while ((unsigned char)(*pReducedFile) != 0xCC && i < 2000)

    char is signed by default. As such, it can't be equal to 0xCC or 204 -  it can be equal to -52 instead.


    Igor Tandetnik

    • Marked as answer by ShimonShami Wednesday, October 26, 2011 7:43 PM
    Tuesday, October 11, 2011 1:52 PM

All replies

  •  if ((*pReducedFile) == 204)
      break;
    }

    while the ReducedFile is an array of chars with 2000 chars.

    My problem is  when the pointer reaches the point that it should break i.e. on the if condition: if ((*pReducedFile) == 0xCC)or the other one if ((*pReducedFile) == 204) while it has the value of CC it retunes false on the condition when it has to break.

    What is the type of pReducedFile?
    Is it perhaps a pointer to a Unicode character?

    Dave

    Tuesday, October 11, 2011 11:50 AM
  • Hi Dave,

     

    That pointer is defined as a char

    i.e. char *pReducedFile, ReducedFile[2000];

    pReducedFile = ReducedFile;

    by the way I've tried to do the if condition as follows and it gave me the wrong answer, i.e. it didn't fail into the break:

    if (ReducedFile[counter] == 0xCC)

     

    Waiting,

    Shimon.

    Tuesday, October 11, 2011 12:09 PM
  • Try the while condition as follow:

    while ((*pReducedFile != 0xCC) && i < 2000)
    
    

    So you can get rid of the IF tests inside the loop. Also, initialize i before the loop.

    If it doesn't break, maybe it means the buffer doesn't contain 0xCC?

     

     


    Tuesday, October 11, 2011 12:19 PM
  • Try the while condition as follow:

    while ((*pReducedFile != 0xCC) && i < 2000)
    
    

    So you can get rid of the IF tests inside the loop. Also, initialize i before the loop.

    If it doesn't break, maybe it means the buffer doesn't contain 0xCC?

     

     


    Hi Pierre,

     

    Thanks Pierre but:

    First: the buffer has that value i.e. 0xCC.

    Second: I've also did it before but it only stopped at i = 2000.

    while it had from 1500 the values of 0xCC
    Moreover, when I'm going step by step with the debugger I can see that it gets the value but it gives on the if condition a false value when it has to give a true value and break of the while loop.

     

    Bye,

    Shimon.

    Tuesday, October 11, 2011 1:00 PM
  • ShimonShami wrote:

    while ((*pReducedFile != 0xCC) || i < 2000)

    Try

    while ((unsigned char)(*pReducedFile) != 0xCC && i < 2000)

    char is signed by default. As such, it can't be equal to 0xCC or 204 -  it can be equal to -52 instead.


    Igor Tandetnik

    • Marked as answer by ShimonShami Wednesday, October 26, 2011 7:43 PM
    Tuesday, October 11, 2011 1:52 PM
  • Very common error. I see many relevant examples:

     

    shareaza

    void CRemote::Output(LPCTSTR pszName)
    {
    
      ...
      CHAR* pBytes = new CHAR[ nBytes ];
      hFile.Read( pBytes, nBytes );
      ...
      if ( nBytes > 3 && pBytes[0] == 0xEF && pBytes[1] == 0xBB && pBytes[2] == 0xBF )
      {
        pBytes += 3;
        nBytes -= 3;
        bBOM = true;
      }
      ...
    }
    
    PVS-Studio:
    V547 Expression 'pBytes [ 0 ] == 0xEF' is always false. The value range of signed char type: [-128, 127]. Shareaza remote.cpp 350
    V547 Expression 'pBytes [ 1 ] == 0xBB' is always false. The value range of signed char type: [-128, 127]. Shareaza remote.cpp 350
    V547 Expression 'pBytes [ 2 ] == 0xBF' is always false. The value range of signed char type: [-128, 127]. Shareaza remote.cpp 350
    
    

     

    Ultimate ToolBox

    BOOL CMaskData::IsValidInput(TCHAR chNewChar)
    {
       ...
      if((chNewChar >= 128) && (chNewChar <= 255))
        bIsValidInput=TRUE ;
      ...
    }
    
    PVS-Studio:
    V547 Expression 'chNewChar >= 128' is always false. The value range of signed char type: [-128, 127]. UT oxmaskededit.cpp 81
    V547 Expression 'chNewChar <= 255' is always true. The value range of signed char type: [-128, 127]. UT oxmaskededit.cpp 81
    
    

    TortoiseSVN

    CString CPathUtils::PathUnescape (const char* path)
    {
      // try quick path
      size_t i = 0;
      for (; char c = path[i]; ++i)
        if ((c >= 0x80) || (c == '%'))
        {
          // quick path does not work for
          // non-latin or escaped chars
          std::string utf8Path (path);
          CPathUtils::Unescape (&utf8Path[0]);
          return CUnicodeUtils::UTF8ToUTF16 (utf8Path);
        }
      ...
    }
    
    PVS-Studio:
    V547 Expression 'c >= 0x80' is always false. The value range of signed char type: [-128, 127]. pathutils.cpp 559
    
    

    :)


    Andrey Karpov is technical manager of the OOO "Program Verification Systems" (Co Ltd) company developing the PVS-Studio tool which is a package of static code analyzers integrating into the Visual Studio development environment.

    Monday, October 17, 2011 12:39 PM