Click here to Skip to main content
Licence CPOL
First Posted 29 Feb 2004
Views 44,374
Bookmarked 15 times

Pointer Arithmetic and Portable Code

By | 3 Mar 2004 | Article
Avoid writing code that depends on pointer arithmetic to make your code portable.

Introduction

Once, one of my students after completing her degree, went to a job interview and got this question in her test.

    char* pC = "Hello World";

    int* pInt = (int*)pC;
    ++pInt;
    char* pChar = (char*)pInt;

    cout << *pChar << endl;

She was asked to guess the output of this program. She tried her best to answer the question. After coming back home, she contacted me to confirm her understanding. I was surprised to see this code, due to two reasons. First, the cout clearly shows that code is written in C++, so it is not recommended to use C style cast in C++ code [MEY98]. They should use new C++ style cast, so first improvement in the code should be something like this:

    char* pC = "Hello World";

    int* pInt = reinterpret_cast<int*>(pC);
    ++pInt;
    char* pChar = reinterpret_cast<char*>(pInt);

    cout << *pChar << endl;

Although this code is now better than previous one and standard C++ code, which will compile on any standard C++ compiler, it is not portable. The output of this code depends on the platform on which this program will run. According to the Standard of C++, section 3.9.1.2, "Plain integers have the natural size suggested by the architecture of the execution environment." [ISO98].

Well, one might think of using the sizeof operator. Wait before we discuss the problems of sizeof, remember you are doing pointer arithmetic here and addition of 1 in integer pointer is not add one in its address. In addition, the output of sizeof is also not portable across different platforms. According to section 5.3.3 of C++ standard, "the result of sizeof applied to any other fundamental type is implementation defined."[ISO98]. Here, any other means other than char, signed char and unsigned char types.

The increment of pointer is 4 bytes on 32 bits platform and 2 bytes on 16 bits platform. The output of this program is "o" where the size of integer is 4 and "l" where the size of integer is 2 bytes.

This is not limited to character pointer only, in fact the size of bool and wchar_t is also implementation dependent [ISO98] and any code assuming any assumption about its size are not portable.

    char* pC = "Hello World";
    pC += sizeof(int);

    cout << *pC << endl;

And similarly, this code is not portable too:

    char* pC = "Hello World";
    pC += sizeof(bool);

    cout << *pC << endl;

It is even worst when you call a function, which internally uses pointer arithmetic and you pass different types as parameters to it.

void fun(wchar_t* pC)
{
    int iLen = strlen(reinterpret_cast<char*>(pC));
    // do something
}

The value of iLen is one where wchar_t is implemented as multi byte characters instead of the actual length of the string, because NULL is placed after each character of the string. Some situations are even more dangerous when you try to write in memory using pointer arithmetic directly or indirectly. One such example is:

void fun(wchar_t* pC1, wchar_t* pC2)
{
    // Do something
    strcpy(reinterpret_cast<char*>(pC1), 
            reinterpret_cast<char*>(pC2));
    // Do something
}

This code may run correctly on some platforms where char and wchar_t are same but it may crash on some of them where these are not same. Write portable code across all the platforms. Do not assume anything about the size of fundamental types and be careful when using pointer arithmetic.

Thanks to Mahwish Waheed Khan to share her experience and give me example code, which is not portable across platforms.

References

  1. [ISO98] International Standard Programming Language C++ ISO/ICE 14882
  2. [MEY98] Effective C++ 50 specific ways to improve your programs and design, 2nd edition, Scott Meyers

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Zeeshan Amjad

Software Developer (Senior)
Bechtel Corporation
United States United States

Member

Working as a C++ Developer at Bechtel Corporation.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralIt's really C code that used cout instead of printf Pinmemberscienceprogrammer18:43 18 Jan '09  
Generalreinterpret_cast<type *>(...)&(type *)... PinmemberTKD0:25 10 Mar '04  
GeneralRe: reinterpret_cast<type *>(...)&(type *)... PinmemberMike Dimmick1:32 10 Mar '04  
QuestionIs sizeof(char) guaranteed to be one byte/machine word? PinmemberDon Clugston14:14 1 Mar '04  
AnswerRe: Is sizeof(char) guaranteed to be one byte/machine word? Pinmemberedger14:37 1 Mar '04  
GeneralRe: Is sizeof(char) guaranteed to be one byte/machine word? PinmemberDon Clugston17:12 1 Mar '04  
Generalmissing the point of the test PinmemberHarold Bamford5:21 1 Mar '04  
GeneralRe: missing the point of the test PinmemberWREY7:07 1 Mar '04  
GeneralRe: missing the point of the test PinmemberHarold Bamford7:13 1 Mar '04  
GeneralRe: missing the point of the test PinmemberWREY10:52 1 Mar '04  
GeneralRe: missing the point of the test PinmemberJohnTesla11:02 9 Sep '06  
AnswerRe: missing the point of the test PinmemberHarold Bamford5:29 11 Sep '06  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 4 Mar 2004
Article Copyright 2004 by Zeeshan Amjad
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid