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));
}
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)
{
strcpy(reinterpret_cast<char*>(pC1),
reinterpret_cast<char*>(pC2));
}
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
- [ISO98] International Standard Programming Language C++ ISO/ICE 14882
- [MEY98] Effective C++ 50 specific ways to improve your programs and design, 2nd edition, Scott Meyers