C++ Programming: Static Code Analysis and the New Language Standard C++0x – CodeGuru

Posted by: Lava Kafle

This function cannot work on a 64-bit system with large arrays. If the 
variable arraySize has a value more than UINT_MAX, the condition 
"i < arraySize" will never be fulfilled and an eternal loop will 

occur.bool IsPresent(char *array,
               size_t arraySize,
               char key)
  for (unsigned i = 0; i < arraySize; i++) 
    if (array[i] == key)
      return true;
  return false;

warning C4267: 'initializing' : 
conversion from 'size_t' to 'unsigned int', possible loss of data
void f(const vector<int>& a,
       vector<float>& b)
  typedef decltype(a[0]*b[0]) Tmp;
  for (int i=0; i<b.size(); ++i)
    Tmp* p = new Tmp(a[i]*b[i]);
    // ...

This code has a 64-bit error: the function behaves correctly when compiling the Win32 version and fails when the code is built in the Win64 mode. The error is in using the type unsigned for the variable “n”, although the type string::size_type must be used which is returned by the function find(). In the 32-bit program, the types string::size_type and unsigned coincide and we get correct results. In the 64-bit program, string::size_type and unsigned do not coincide any more. When the substring is not found, the function find() returns the value string::npos that equals 0xFFFFFFFFFFFFFFFFui64. This value is cut to the value 0xFFFFFFFFu and placed into a 32-bit variable. As a result, the condition 0xFFFFFFFFu != 0xFFFFFFFFFFFFFFFFui64 is true and we have the situation when the function Find_Incorrect always returns true.

C++ Programming: Static Code Analysis and the New Language Standard C++0x – CodeGuru.

C++ Programming: Static Code Analysis and the New Language Standard C++0x – CodeGuru was last modified: May 12th, 2010 by Lava Kafle

Blog Comments

  1. Status of Experimental C++0x Support in GCC 4.3

    GCC provides experimental support for the upcoming ISO C++ standard, C++0x. This support can be enabled with the -std=c++0x or -std=gnu++0x compiler options; the former disables GNU extensions.
    GCC’s C++0x mode tracks the C++0x working paper drafts produced by the ISO C++ committee, available on the ISO C++ committee’s web site
    Since this standard is still being extended and modified, the feature set provided by the experimental C++0x mode may vary greatly from one GCC version to another. No attempts will be made to preserve backward compatibility with C++0x features whose semantics have changed during the course of C++0x standardization.

  2. Bjarne Stroustrup, inventor of the C++

    Bjarne Stroustrup, inventor of the cplus cplusplus C++ programming language, is a computer scientist and the College of Engineering Chair Professor of Computer Science at Texas A&M University. He has always been highly involved in the standardization of C++. Since 2004, Bjarne and the standards committee have been busy hammering out the details of a new standard, temporarily titled C++0x. Bjarne was gracious enough to take time out of his busy schedule to speak with DevX C++ Pro, Danny Kalev, about new C++0x features and the state of the C++ language.

  3. Exploring the New MSBuild Features for Visual C++ 2010

    In recognition of his work in the .NET area, he was awarded the Microsoft Most Valued Professional Award from 2002 through 2007.
    The move of Visual C++ onto the MSBuild platform brings C++ inline with Microsoft’s other compiler offerings that all use this format. Visual C++ 2010 is deeply integrated into MSBuild, with all elements of the build process fully visible and accessible in the new VCXPROJ project file format.

    MSBuild is a documented, powerful, and extensible build engine that far exceeds the capabilities of the previous Visual C++ build engine. MSBuild takes Items and associated Properties as inputs into a Task, and Tasks are grouped together into Targets to form a high-level build activity. MSBuild will be welcomed by C++ developers who miss the power and expressiveness of MAK files, and for developers who have yet to explore the world of build customization and extension, the introduction of MSBuild as the project file format for Visual C++ 2010 represents the best time to get started.
    Nick Wienholt is an independent Windows and .NET consultant based in Sydney. He is the author of Maximizing .NET Performance and co-author of A Programmers Introduction to C# 2.0 from Apress, and specialises in system-level software architecture and development, with a particular focus of performance, security, interoperability, and debugging.

Post Your Comments:

Your email address will not be published. Required fields are marked *