Any C++ poets around? What da'ya think?

Top Page
Attachments:
Message as email
+ (text/plain)
+ msg.txt (text/plain)
+ (text/plain)
Delete this message
Reply to this message
Author: kitepilot@kitepilot.com
Date:  
To: Main PLUG discussion list
Old-Topics: Re: East side PLUG meeting-NEW LOCATION-Info attached
Subject: Any C++ poets around? What da'ya think?


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@towplane$ g++     -o NULL_dereference.g++     NULL_dereference.cpp
wiseuser@towplane$ ./NULL_dereference.g++;echo $?
I am a GOOD boy.
Segmentation fault
139
wiseuser@towplane$




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

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


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