Why does this work?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Why does this work?

  1. #1
    Join Date
    Apr 2010
    Posts
    2

    Why does this work?

    A dynamic object but we assign it the value NULL...So our object is NULL but we can still call the member function showthis() and it works, but why?

    #include <iostream>

    class myc
    {
    public:
    myc() {}
    ~myc() {}
    void showthis() const {std::cout<<"Hello, World!"<<"\n";}
    private:
    };

    int main(int argc, char**argv)
    {
    myc *thec = (myc*)NULL;

    std::cout<<"address of thec->"<<(void*)thec<<"\n";
    thec->showthis();

    return 0;
    }

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,891

    Re: Why does this work?

    "object is NULL" ---- Actually it's a pointer that's NULL. There is no object here.

    As to why it works, simple: C++ offers only guarantees about what will work. It promises nothing about what will not work. So just because something appears to be doing what you intend does not mean the program is correct!

    In this case, since the method doesn't do anything with the "this" pointer, the fact that that pointer is garbage doesn't matter. You cannot be assured of something like this always working, but that's the most likely explanation for the behavior you're seeing.

  3. #3
    Join Date
    Apr 2010
    Posts
    2

    Re: Why does this work?

    Heh, thanks. But it was more of a test than a question.
    I need you to explain why it's working and doing what it does.

  4. #4
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,355

    Re: Why does this work?

    Quote Originally Posted by Werpd
    But it was more of a test than a question.
    A test? Then surely you already know the answer?

    Quote Originally Posted by Werpd
    I need you to explain why it's working and doing what it does.
    It does not work. It appears to work because, as Lindley noted, the member function "doesn't do anything with the "this" pointer".
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  5. #5
    Join Date
    Oct 2007
    Posts
    132

    Re: Why does this work?

    Yes I believe the fact that it doesn't use "this" is correct -- more specifically, because it does not use *any* other class members (which would require using "this"). Basically thec->showthis() is equivalent to saying:

    showthis(thec);

    ...where "thec" is a hidden parameter passed as "this" to all non-static class member functions when invoked....so from there it goes to show that showthis() is just fine until you actually attempt to modify non-static data which would require calling up the specific instance of the class in question (i.e. thec/this)...that's where you get the fun screen.

  6. #6
    Join Date
    Apr 1999
    Posts
    27,444

    Re: Why does this work?

    Quote Originally Posted by Werpd View Post
    Heh, thanks. But it was more of a test than a question.
    I need you to explain why it's working and doing what it does.
    And what if I have a compiler where this code doesn't "work"? Do I need to explain that also?

    You have constructs in C++ that are considered "undefined behaviour" (UB). The code you have is UB, and there is no need to explain anything. That code could run on that compiler using a certain option, and crap out if another compiler option is used. Someone could develop a compiler that could detect code as you've posted, and just issue a diagnostic that the program cannot continue when you run the code.

    C++ isn't like any of the other languages, where bad code automatically produces a diagnostic, i.e. similar to Java or C#. Bad code in C++ (or C) means your program can do anything in particular, including run successfully or crash and burn right away.

    Regards,

    Paul McKenzie

  7. #7
    Join Date
    Jan 2009
    Posts
    1,689

    Re: Why does this work?

    Because it's so easy to accidentally call a method on a null pointer, I have found it useful to do this:
    Code:
    #ifdef DEBUG
         #define NULLSAFE()  assert(this)
    #else
         #define NULLSAFE()
    #endif
    Since I use GCC almost exclusively, I set pointers to NULL in debug mode to catch those situations.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center