SlideShare a Scribd company logo
In what way can C++0x standard help
you eliminate 64-bit errors
Author: Andrey Karpov

Date: 28.02.2010

Programmers see in C++0x standard an opportunity to use lambda-functions and other entities I do not
quite understand :). But personally I see convenient means in it that allow us to get rid of many 64-bit
errors.

Consider a function that returns "true" if at least one string contains the sequence "ABC".

typedef vector<string> ArrayOfStrings;

bool Find_Incorrect(const ArrayOfStrings &arrStr)

{

    ArrayOfStrings::const_iterator it;

    for (it = arrStr.begin(); it != arrStr.end(); ++it)

    {

        unsigned n = it->find("ABC");

        if (n != string::npos)

         return true;

    }

    return false;

};

This function is correct when compiling the Win32 version but fails when building the application in
Win64. mode. Consider another example of using the function:

#ifdef IS_64

    const char WinXX[] = "Win64";

#else

    const char WinXX[] = "Win32";

#endif

int _tmain(int argc, _TCHAR* argv[])

{
ArrayOfStrings array;

    array.push_back(string("123456"));

    array.push_back(string("QWERTY"));

    if (Find_Incorrect(array))

        printf("Find_Incorrect (%s): ERROR!n", WinXX);

    else

        printf("Find_Incorrect (%s): OK!n", WinXX);

    return 0;

}

Find_Incorrect (Win32): OK!

Find_Incorrect (Win64): ERROR!

The error here is related to choosing the type "unsigned" for "n" variable although the function find()
returns the value of string::size_type type. In the 32-bit program, the types string::size_type and
unsigned coincide and we get the correct result.

In the 64-bit program, these types do not coincide. As the substring is not found, the function find()
returns the value string::npos that equals 0xFFFFFFFFFFFFFFFFui64. This value gets cut to 0xFFFFFFFFu
and is written into the 32-bit variable. As a result, the condition 0xFFFFFFFFu ==
0xFFFFFFFFFFFFFFFFui64 is always false and we get the message "Find_Incorrect (Win64): ERROR!".

We may correct the code using the type string::size_type.

bool Find_Correct(const ArrayOfStrings &arrStr)

{

    ArrayOfStrings::const_iterator it;

    for (it = arrStr.begin(); it != arrStr.end(); ++it)

    {

        string::size_type n = it->find("ABC");

        if (n != string::npos)

          return true;

    }

    return false;

};
Now the code works as it should though it is too long and not very nice to constantly add the type
string::size_type. You may redefine it through typedef but still it looks somehow complicated. Using
C++0x we can make the code much smarter and safer.

Let us use the key word "auto" to do that. Earlier, this word meant that the variable was created on the
stack and it was implied if you had not specified something different, for example, register. Now the
compiler identifies the type of a variable defined as "auto" on its own, relying on the function initializing
this variable.

Note that an auto-variable cannot store values of different types during one instance of program
execution. C++ remains a static typified language and "auto" only makes the compiler identify the type
on its own: once the variable is initialized, its type cannot be changed.

Let us use the key word "auto" in our code. The project was created in Visual Studio 2005 while C++0x
standard gets supported only beginning with Visual Studio 2010. So I chose Intel C++ compiler included
into Intel Parallel Studio 11.1 and supporting C++0x standard to perform compilation. The option of
enabling C++0x support is situated in the Language section and reads "Enable C++0x Support". As you
may see in Figure 1, this option is Intel Specific.




Figure 1 - Support of C++0x standard

The modified code looks as follows:

bool Find_Cpp0X(const ArrayOfStrings &arrStr)

{

    for (auto it = arrStr.begin(); it != arrStr.end(); ++it)

    {
auto n = it->find("ABC");

        if (n != string::npos)

         return true;

    }

    return false;

};

Consider the way the variable "n" is defined now. Smart, isn't it? It also eliminates some errors including
64-bit ones. The variable "n" will have exactly the same type returned by the function find(), i.e.
string::size_type. Note also that there is no string with the iterator definition:

ArrayOfStrings::const_iterator it;

It is not very smart to define the variable "it" inside the loop (for it is rather lengthy). So the definition
was taken out of the loop. Now the code is short and accurate:

for (auto it = arrStr.begin(); ......)

Let us examine one more key word "decltype". It allows you to define the type of a variable relying on
the type of another variable. If we had to define all the variables in our code beforehand, we could write
it in this way:

bool Find_Cpp0X_2(const ArrayOfStrings &arrStr)

{

    decltype(arrStr.begin()) it;

    decltype(it->find("")) n;

    for (it = arrStr.begin(); it != arrStr.end(); ++it)

    {

        n = it->find("ABC");

        if (n != string::npos)

          return true;

    }

    return false;

};

Of course, it is senseless in our case but may be useful in some others.

Unfortunately (or fortunately for us :-), the new standard does not eliminate already existing defects in
the code despite really simplifying the process of writing safe 64-bit code. To be able to fix an error with
the help of memsize-тип or "auto" you must find this error at first. So, the tool Viva64 will not become
less relevant with the appearance of standard C++0x.
P.S.
You may download the project with the code here.

More Related Content

PPT
PPTX
C Programming Language Step by Step Part 3
PDF
Lesson 9. Pattern 1. Magic numbers
PDF
05 Jo P May 07
PPT
C++ Introduction
PPT
Lecture03
PPTX
1.3 core programming [identify the appropriate method for handling repetition]
PDF
Analysis of the Ultimate Toolbox project
C Programming Language Step by Step Part 3
Lesson 9. Pattern 1. Magic numbers
05 Jo P May 07
C++ Introduction
Lecture03
1.3 core programming [identify the appropriate method for handling repetition]
Analysis of the Ultimate Toolbox project

What's hot (19)

PDF
Codejunk Ignitesd
PDF
PVS-Studio documentation (version 4.54)
PDF
[C++ Korea] Effective Modern C++ Study, Item 11 - 13
PPTX
C++ 11 Features
PDF
Lesson 17. Pattern 9. Mixed arithmetic
PDF
(5) cpp dynamic memory_arrays_and_c-strings
PPTX
CPP Programming Homework Help
PDF
Checking WinMerge with PVS-Studio for the second time
PDF
Programs that work in c and not in c
PDF
PVS-Studio team is about to produce a technical breakthrough, but for now let...
PDF
JavaScript ES10 and React Js Introduction
PDF
Regular types in C++
PDF
Analyzing the Dolphin-emu project
PDF
C++17 introduction - Meetup @EtixLabs
PDF
C++11
PPTX
PDF
Memory management in C++
DOC
C operators
Codejunk Ignitesd
PVS-Studio documentation (version 4.54)
[C++ Korea] Effective Modern C++ Study, Item 11 - 13
C++ 11 Features
Lesson 17. Pattern 9. Mixed arithmetic
(5) cpp dynamic memory_arrays_and_c-strings
CPP Programming Homework Help
Checking WinMerge with PVS-Studio for the second time
Programs that work in c and not in c
PVS-Studio team is about to produce a technical breakthrough, but for now let...
JavaScript ES10 and React Js Introduction
Regular types in C++
Analyzing the Dolphin-emu project
C++17 introduction - Meetup @EtixLabs
C++11
Memory management in C++
C operators
Ad

Similar to In what way can C++0x standard help you eliminate 64-bit errors (20)

PDF
Static code analysis and the new language standard C++0x
PDF
Static code analysis and the new language standard C++0x
PDF
C++11 and 64-bit Issues
PDF
Tesseract. Recognizing Errors in Recognition Software
PDF
"Why is there no artificial intelligence yet?" Or, analysis of CNTK tool kit ...
PDF
Anomalies in X-Ray Engine
PDF
Arrays and strings in c++
PDF
Big Brother helps you
PDF
Lesson 24. Phantom errors
PDF
Analysis of the Trans-Proteomic Pipeline (TPP) project
PDF
Lesson 13. Pattern 5. Address arithmetic
PDF
Optimization in the world of 64-bit errors
PPTX
the refernce of programming C notes ppt.pptx
PDF
Analysis of Microsoft Code Contracts
PDF
How to avoid bugs using modern C++
PDF
Analysis of Haiku Operating System (BeOS Family) by PVS-Studio. Part 1
PPTX
CPP Homework Help
PDF
Checking 7-Zip with PVS-Studio analyzer
PDF
Headache from using mathematical software
PDF
The article is a report about testing of portability of Loki library with 64-...
Static code analysis and the new language standard C++0x
Static code analysis and the new language standard C++0x
C++11 and 64-bit Issues
Tesseract. Recognizing Errors in Recognition Software
"Why is there no artificial intelligence yet?" Or, analysis of CNTK tool kit ...
Anomalies in X-Ray Engine
Arrays and strings in c++
Big Brother helps you
Lesson 24. Phantom errors
Analysis of the Trans-Proteomic Pipeline (TPP) project
Lesson 13. Pattern 5. Address arithmetic
Optimization in the world of 64-bit errors
the refernce of programming C notes ppt.pptx
Analysis of Microsoft Code Contracts
How to avoid bugs using modern C++
Analysis of Haiku Operating System (BeOS Family) by PVS-Studio. Part 1
CPP Homework Help
Checking 7-Zip with PVS-Studio analyzer
Headache from using mathematical software
The article is a report about testing of portability of Loki library with 64-...
Ad

Recently uploaded (20)

PDF
Electronic commerce courselecture one. Pdf
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PPTX
Spectroscopy.pptx food analysis technology
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Encapsulation theory and applications.pdf
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
Big Data Technologies - Introduction.pptx
PDF
KodekX | Application Modernization Development
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
cuic standard and advanced reporting.pdf
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PPTX
MYSQL Presentation for SQL database connectivity
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PPTX
Cloud computing and distributed systems.
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
DOCX
The AUB Centre for AI in Media Proposal.docx
Electronic commerce courselecture one. Pdf
Advanced methodologies resolving dimensionality complications for autism neur...
Understanding_Digital_Forensics_Presentation.pptx
Spectroscopy.pptx food analysis technology
MIND Revenue Release Quarter 2 2025 Press Release
Programs and apps: productivity, graphics, security and other tools
Encapsulation theory and applications.pdf
Building Integrated photovoltaic BIPV_UPV.pdf
20250228 LYD VKU AI Blended-Learning.pptx
Big Data Technologies - Introduction.pptx
KodekX | Application Modernization Development
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
cuic standard and advanced reporting.pdf
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
MYSQL Presentation for SQL database connectivity
NewMind AI Weekly Chronicles - August'25 Week I
Cloud computing and distributed systems.
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Mobile App Security Testing_ A Comprehensive Guide.pdf
The AUB Centre for AI in Media Proposal.docx

In what way can C++0x standard help you eliminate 64-bit errors

  • 1. In what way can C++0x standard help you eliminate 64-bit errors Author: Andrey Karpov Date: 28.02.2010 Programmers see in C++0x standard an opportunity to use lambda-functions and other entities I do not quite understand :). But personally I see convenient means in it that allow us to get rid of many 64-bit errors. Consider a function that returns "true" if at least one string contains the sequence "ABC". typedef vector<string> ArrayOfStrings; bool Find_Incorrect(const ArrayOfStrings &arrStr) { ArrayOfStrings::const_iterator it; for (it = arrStr.begin(); it != arrStr.end(); ++it) { unsigned n = it->find("ABC"); if (n != string::npos) return true; } return false; }; This function is correct when compiling the Win32 version but fails when building the application in Win64. mode. Consider another example of using the function: #ifdef IS_64 const char WinXX[] = "Win64"; #else const char WinXX[] = "Win32"; #endif int _tmain(int argc, _TCHAR* argv[]) {
  • 2. ArrayOfStrings array; array.push_back(string("123456")); array.push_back(string("QWERTY")); if (Find_Incorrect(array)) printf("Find_Incorrect (%s): ERROR!n", WinXX); else printf("Find_Incorrect (%s): OK!n", WinXX); return 0; } Find_Incorrect (Win32): OK! Find_Incorrect (Win64): ERROR! The error here is related to choosing the type "unsigned" for "n" variable although the function find() returns the value of string::size_type type. In the 32-bit program, the types string::size_type and unsigned coincide and we get the correct result. In the 64-bit program, these types do not coincide. As the substring is not found, the function find() returns the value string::npos that equals 0xFFFFFFFFFFFFFFFFui64. This value gets cut to 0xFFFFFFFFu and is written into the 32-bit variable. As a result, the condition 0xFFFFFFFFu == 0xFFFFFFFFFFFFFFFFui64 is always false and we get the message "Find_Incorrect (Win64): ERROR!". We may correct the code using the type string::size_type. bool Find_Correct(const ArrayOfStrings &arrStr) { ArrayOfStrings::const_iterator it; for (it = arrStr.begin(); it != arrStr.end(); ++it) { string::size_type n = it->find("ABC"); if (n != string::npos) return true; } return false; };
  • 3. Now the code works as it should though it is too long and not very nice to constantly add the type string::size_type. You may redefine it through typedef but still it looks somehow complicated. Using C++0x we can make the code much smarter and safer. Let us use the key word "auto" to do that. Earlier, this word meant that the variable was created on the stack and it was implied if you had not specified something different, for example, register. Now the compiler identifies the type of a variable defined as "auto" on its own, relying on the function initializing this variable. Note that an auto-variable cannot store values of different types during one instance of program execution. C++ remains a static typified language and "auto" only makes the compiler identify the type on its own: once the variable is initialized, its type cannot be changed. Let us use the key word "auto" in our code. The project was created in Visual Studio 2005 while C++0x standard gets supported only beginning with Visual Studio 2010. So I chose Intel C++ compiler included into Intel Parallel Studio 11.1 and supporting C++0x standard to perform compilation. The option of enabling C++0x support is situated in the Language section and reads "Enable C++0x Support". As you may see in Figure 1, this option is Intel Specific. Figure 1 - Support of C++0x standard The modified code looks as follows: bool Find_Cpp0X(const ArrayOfStrings &arrStr) { for (auto it = arrStr.begin(); it != arrStr.end(); ++it) {
  • 4. auto n = it->find("ABC"); if (n != string::npos) return true; } return false; }; Consider the way the variable "n" is defined now. Smart, isn't it? It also eliminates some errors including 64-bit ones. The variable "n" will have exactly the same type returned by the function find(), i.e. string::size_type. Note also that there is no string with the iterator definition: ArrayOfStrings::const_iterator it; It is not very smart to define the variable "it" inside the loop (for it is rather lengthy). So the definition was taken out of the loop. Now the code is short and accurate: for (auto it = arrStr.begin(); ......) Let us examine one more key word "decltype". It allows you to define the type of a variable relying on the type of another variable. If we had to define all the variables in our code beforehand, we could write it in this way: bool Find_Cpp0X_2(const ArrayOfStrings &arrStr) { decltype(arrStr.begin()) it; decltype(it->find("")) n; for (it = arrStr.begin(); it != arrStr.end(); ++it) { n = it->find("ABC"); if (n != string::npos) return true; } return false; }; Of course, it is senseless in our case but may be useful in some others. Unfortunately (or fortunately for us :-), the new standard does not eliminate already existing defects in the code despite really simplifying the process of writing safe 64-bit code. To be able to fix an error with the help of memsize-тип or "auto" you must find this error at first. So, the tool Viva64 will not become less relevant with the appearance of standard C++0x.
  • 5. P.S. You may download the project with the code here.