Any C++ poets around? What da'ya think?
kitepilot at kitepilot.com
kitepilot at kitepilot.com
Fri May 7 06:41:26 MST 2010
-------------- next part --------------
Look at the following jewel of code and throw at will your best guess:
What will the output be?
(scroll down for *MY* answer... :)
/*****************************************************************************
* g++ -o NULL_dereference.g++ NULL_dereference.cpp
* xlc++_r -o NULL_dereference.xlc++_r NULL_dereference.cpp
*****************************************************************************/
#include <iostream>
#include <string.h>
void RunTest(char *AmIaNullPtr);
int main(int argc, char **argv)
{
char GoodBoy[] = "I am a GOOD boy.",
*BadBoy = NULL;
int Result = 0;
RunTest(GoodBoy);
RunTest(BadBoy);
return Result;
}
void RunTest(char *AmIaNullPtr)
{
switch (*AmIaNullPtr)
{
case '\0':
std::cout << "I seem to be NULL" << std::endl;
break;
default :
std::cout << AmIaNullPtr << std::endl;
break;
}
}
/*END*/
Surprise, surprise, if you answered:
"it will blow up",
(like I did)
you are right *IF*:
You compile it and run it under Linux (Ubuntu 10.04 to be precise)
The output will look like:
wiseuser at towplane$ g++ -o NULL_dereference.g++ NULL_dereference.cpp
wiseuser at towplane$ ./NULL_dereference.g++;echo $?
I am a GOOD boy.
Segmentation fault
139
wiseuser at towplane$
But if you however compile it in:
wiseuser at AIXhost$ uname -a
AIX AIXhost 3 5 00CE5B734C00
wiseuser at AIXhost$
Your output will be:
wiseuser at AIXhost$ xlc++_r -o NULL_dereference.xlc++_r NULL_dereference.cpp
wiseuser at AIXhost$ ./NULL_dereference.xlc++_r;echo $?
I am a GOOD boy.
I seem to be NULL
0
wiseuser at AIXhost$
Beats me... :(
ET
More information about the PLUG-discuss
mailing list