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:

wiseu...@towplane$ g++     -o NULL_dereference.g++     NULL_dereference.cpp
wiseu...@towplane$ ./NULL_dereference.g++;echo $?
I am a GOOD boy.
Segmentation fault
139
wiseu...@towplane$



But if you however compile it in:
wiseu...@aixhost$ uname -a
AIX AIXhost 3 5 00CE5B734C00
wiseu...@aixhost$

Your output will be:
wiseu...@aixhost$ xlc++_r -o NULL_dereference.xlc++_r NULL_dereference.cpp
wiseu...@aixhost$ ./NULL_dereference.xlc++_r;echo $?
I am a GOOD boy.
I seem to be NULL
0
wiseu...@aixhost$


Beats me...   :(
ET
---------------------------------------------------
PLUG-discuss mailing list - [email protected]
To subscribe, unsubscribe, or to change your mail settings:
http://lists.PLUG.phoenix.az.us/mailman/listinfo/plug-discuss

Reply via email to