Writing Portable Code in C++'�Variable Scope

Environment: C++

Writing portable code is an interesting as well as a challenging task sometimes. You have to restrict yourself not only to the standard of the language but sometimes also to the implementation of the language and its libraries on different platforms. Sometimes the standard of language doesn't say anything about the implementation of some features and lets the vendor implement it in their own way. The most suitable example of this that comes to mind is the sort algorithm of the standard library. The C++ standard [ISO98] doesn't say anything about which algorithm should be used for sorting; therefore, vendors are free to use any sort function that fulfills the requirements of the standard.

Other problems that may come during the writing of portable code are different levels of compliance with the standards of different compilers. Suppose you are working in an environment in which your team uses different compilers to compile a project. Sometimes it becomes tricky, if not impossible, to write code that can run on all compilers. The ideal solution of this problem is to use the same compiler, which is as compliant with the standard as possible. But, in the real world, it is not possible due to lots of factors such as personal preference, license issues, productivity, optimization, and so forth. One example of such a problem is the scope of variables in a loop.

According to the C++ standard, the scope of the variable that declares inside the "for" loop is limited to the loop; in other words, this code is legal according to the C++ standard, but some compilers, such as Visual C++ 6, can't compile it successfully.

const int iMax = 100;
for (int iIndex = 0; iIndex < iMax; ++iIndex)
{
  // do something
}

for (int iIndex = 0; iIndex < iMax; ++iIndex)
{
  // do something
}

And, if we remove the declaration of the variable inside the "for" loop, this code not only becomes illegal but also can't compile on standard C++ compilers such as Comeau C/C++.

const int iMax = 10;
for (int iIndex = 0; iIndex < iMax; ++iIndex)
{
  // do something
}

for (iIndex = 0; iIndex < iMax; ++iIndex)
{
  // do something
}

The easiest solution yo this problem is to put the variable declaration outside the scope of the for loop to make all compilers happy.

const int iMax = 10;
int iIndex;

for (iIndex = 0; iIndex < iMax; ++iIndex)
{
  // do something
}

for (iIndex = 0; iIndex < iMax; ++iIndex)
{
  // do something
}

There is one problem with this technique—the scope of the iIndex variable. Now, this variable becomes visible on that part of code where it shouldn't. There is an interesting technique discussed in [DEV02] to limit the scope as well as make both compilers happy. The technique is to just put extra braces around the "for" loop to limit its scope visible inside the loop.

const int iMax = 10;
{for (int iIndex = 0; iIndex < iMax; ++iIndex)
{
  // do something
}}

{for (int iIndex = 0; iIndex < iMax; ++iIndex)
{
  // do something
}}

These are a few reasons why writing portable code is challenging as well as fun. So, be sure to check your code on different implementations on different platform if you want to make it portable, even if it is written according to a language standard, because different implementations may behave differently on your code although they follow the rules of a standard language.

See Writing Portable Code in C++—A Vectors and String Issue for more issues regarding the writing of portable code.

References

  1. [DEW02] C++ Gotchas, Avoiding Common Problems in Coding and Design. Stephen Dewhurst 2002
  2. [ISO98] International Standard Programming Language C++ ISO/ICE 14882 1998
  3. [JOS99] The C++ Standard Library: A Tutorial and Reference. Nicolai M. Josuttis 1999



About the Author

Zeeshan Amjad

C++ Developer at Bechtel Corporation. zamjad.wordpress.com

Comments

  • java programming

    Posted by abhi78_online on 03/31/2005 07:50am

    code for displaying the number of objects in a program

    Reply
  • What?!?!?!

    Posted by Legacy on 12/14/2003 12:00am

    Originally posted by: ANSI SHMANSI

    You're out of touch with the realities of modern software engineering.

    "Platform independent" is mostly mythological in this day and age.

    The reality is that any platform that does not confirm to and compile code that was targeted at MSVC++ will not survive as a platform to begin with.

    ANSI SHMANSI

    Reply
  • Who need portable Code!!

    Posted by Legacy on 12/11/2003 12:00am

    Originally posted by: EF

    Who need portable code...?
    
    

    On the Microsoft Windows Platform programmers knows, or should knows, that the Microsoft VC++ compiler is the references and it is the compiler that generate the most efficient machine code...

    And this is not my assumption!

    Major games companies that are very anxious to do the most efficient and performant code, because games software needs speed and performance, always compile the final version of their software with Microsoft VC++. This is not from me... This is from the chief programer of the company that does the game named "Myth"... He tool us this at a CGDC confernce a couple of year ago!

    The software engenier of the Maxis (SimCity 2003) told us somthing like this to by comparing the generated code between differente compiler.

    Cross platform for What?? Linux... Duu!
    Amiga.... Nooo!! Unix... Duu!... MacOs? Ok may be the MacOS even if only 7% percent of the people had one!!

    Play station, XBox, GameCube : Yeah ok... may be!!

    About the "for" loop ... we should always could be able to declare the "index" in the for... and this index sould only been get in the "for" scoop... In VC++ the index delcared in a for loop has the scope of the method... And it should not!!!

    I know, i am a little out of the subject. I am sorry!

    Ciao!

    EF

    Reply
  • Here's the quickest way to always be compatible

    Posted by Legacy on 12/11/2003 12:00am

    Originally posted by: Jason King

    Add the following code to one of your base header files:

    #undef for
    #define for if (0) ; else for

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • On-demand Event Event Date: July 22, 2015 Today's increasingly mobile workforce creates new challenges for organizations that rely heavily on collaboration. Businesses need to be both secure and collaborative, and they can't sacrifice one over the other. The plethora of devices and clouds used by mobile employees only complicates these challenges. Watch this webcast and learn how your business can enable mobile access to files from anywhere while it helps to maintain information governance, compliance, and …

  • Following an IT incident through to resolution is more than just acknowledging an alert and restarting a server. The recent State of On-Call Report found that it takes most companies an average of 10-30 minutes to resolve an incident with an average of 5 people involved. Imagine how much money and time companies are spending to deal with incident resolution. But what if you had a tool that offered solutions baked in? Or a tool that sent alerts to the right person or team every time? These are the kind of …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date