In what way can C++0x standard help you eliminate 64-bit  errors
Upcoming SlideShare
Loading in...5
×
 

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

on

  • 231 views

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 ...

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.

Statistics

Views

Total Views
231
Views on SlideShare
231
Embed Views
0

Actions

Likes
0
Downloads
0
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

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

  • In what way can C++0x standard helpyou eliminate 64-bit errorsAuthor: Andrey KarpovDate: 28.02.2010Programmers see in C++0x standard an opportunity to use lambda-functions and other entities I do notquite understand :). But personally I see convenient means in it that allow us to get rid of many 64-biterrors.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 inWin64. mode. Consider another example of using the function:#ifdef IS_64 const char WinXX[] = "Win64";#else const char WinXX[] = "Win32";#endifint _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 andunsigned 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 0xFFFFFFFFuand 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 typestring::size_type. You may redefine it through typedef but still it looks somehow complicated. UsingC++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 thestack and it was implied if you had not specified something different, for example, register. Now thecompiler identifies the type of a variable defined as "auto" on its own, relying on the function initializingthis variable.Note that an auto-variable cannot store values of different types during one instance of programexecution. C++ remains a static typified language and "auto" only makes the compiler identify the typeon 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++0xstandard gets supported only beginning with Visual Studio 2010. So I chose Intel C++ compiler includedinto Intel Parallel Studio 11.1 and supporting C++0x standard to perform compilation. The option ofenabling C++0x support is situated in the Language section and reads "Enable C++0x Support". As youmay see in Figure 1, this option is Intel Specific.Figure 1 - Support of C++0x standardThe 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, isnt it? It also eliminates some errors including64-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 definitionwas 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 onthe type of another variable. If we had to define all the variables in our code beforehand, we could writeit 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 inthe code despite really simplifying the process of writing safe 64-bit code. To be able to fix an error withthe help of memsize-тип or "auto" you must find this error at first. So, the tool Viva64 will not becomeless relevant with the appearance of standard C++0x.
  • P.S.You may download the project with the code here.