Click here to Skip to main content
15,867,308 members
Articles / Programming Languages / C++
Article

Pointer Arithmetic and Portable Code

Rate me:
Please Sign up or sign in to vote.
2.73/5 (11 votes)
3 Mar 2004CPOL3 min read 62K   15   12
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)


Written By
Team Leader American Institute for Research
United States United States
Working as a Team leader in American Institute for Research

Comments and Discussions

 
GeneralIt's really C code that used cout instead of printf Pin
scienceprogrammer18-Jan-09 18:43
scienceprogrammer18-Jan-09 18:43 
Generalreinterpret_cast&lt;type *&gt;(...)&amp;(type *)... Pin
TKD10-Mar-04 0:25
TKD10-Mar-04 0:25 
GeneralRe: reinterpret_cast&lt;type *&gt;(...)&amp;(type *)... Pin
Mike Dimmick10-Mar-04 1:32
Mike Dimmick10-Mar-04 1:32 
QuestionIs sizeof(char) guaranteed to be one byte/machine word? Pin
Don Clugston1-Mar-04 14:14
Don Clugston1-Mar-04 14:14 
AnswerRe: Is sizeof(char) guaranteed to be one byte/machine word? Pin
rockonedge1-Mar-04 14:37
rockonedge1-Mar-04 14:37 
GeneralRe: Is sizeof(char) guaranteed to be one byte/machine word? Pin
Don Clugston1-Mar-04 17:12
Don Clugston1-Mar-04 17:12 
Generalmissing the point of the test Pin
Harold Bamford1-Mar-04 5:21
Harold Bamford1-Mar-04 5:21 
GeneralRe: missing the point of the test Pin
WREY1-Mar-04 7:07
WREY1-Mar-04 7:07 
GeneralRe: missing the point of the test Pin
Harold Bamford1-Mar-04 7:13
Harold Bamford1-Mar-04 7:13 
GeneralRe: missing the point of the test Pin
WREY1-Mar-04 10:52
WREY1-Mar-04 10:52 
GeneralRe: missing the point of the test Pin
JohnTesla9-Sep-06 11:02
JohnTesla9-Sep-06 11:02 
AnswerRe: missing the point of the test Pin
Harold Bamford11-Sep-06 5:29
Harold Bamford11-Sep-06 5:29 

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

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