-5

consider following example:

   // someLibrary.h  which is exported.
    struct HandlePrivate;
    typedef HandlePrivate&  Handle; 

    Handle getHandle(int code);
    void closeHandle(Handle handle);


   // someLibrary.cpp
   #include "someLibrary.h"
   struct HandlePrivate{};//definition of HandlePrivate;
   Handle getHandle(int code)
   {
        static HandlePrivate instance;
        return  code >= 0 
                ? instance 
                :  ( *(HandlePrivate*)0); // Hack, I know it's UB.
   }

Now, other programmer uses this library:

    // userSource.cpp
    #include "someLibrary.h"

    void foo(int code)
    {

        Handle h = getHandle(code);
        // some user code

       closeHandle(h);
    }

Question: When is may break user code (crash or something bad), if "SomeLibrary" author uses that hack (UB) ?

if user uses only Windows and Visual Studio 2010, answer is changed ?

Because, following example is not crashed !!! :

#include <functional>
#include <type_traits>
#include "someLibrary.h"

void foo(int code)
{
     typedef std::reference_wrapper< std::remove_reference<Handle>::type> reference;

     reference h = getHandle(code);

     closeHandle(h); // is not crash, if code = -1     !!!!!!!!!!  

// However, std::reference_wrapper::operator &() - uses dereference to pointer of 0. }

Khurshid
  • 2,654
  • 2
  • 21
  • 29
  • My question isn't why dereferencing hte null pointer become undefined behavior!!! My question is : when it may break user code for above situation ? – Khurshid Jul 17 '14 at 17:40
  • 1
    @Khurshid And how is the answer to that question not the same? Code may break whenever there's undefined behavior, and dereferencing a null pointer is undefined behavior. – Praetorian Jul 17 '14 at 17:42
  • @Praetorian: could you write code, which result is crashed, if you use "someLibrary" ? – Khurshid Jul 17 '14 at 17:44
  • *Is it possible?* Anything's possible when you have undefined behavior, so I'd say yes. *Will it happen?* No idea, I haven't bothered with researching the vagaries of null pointer dereferencing. – Praetorian Jul 17 '14 at 17:48
  • myself can't found any possible situation, when null pointer dereferencing may crash for above situation, for that reason I wrote question here. I perfectly know, dereferencing null pointer is UB. – Khurshid Jul 17 '14 at 17:51
  • A possible situation would be Microsoft releasing a new version of their compiler (or a new version of Windows) that causes a crash when this particular type of undefined behavior occurs. – Jeremy Friesner Jul 17 '14 at 18:49

1 Answers1

0

When a pointer is set to zero, it is pointing to something at address zero. The address could be physical address 0 or logical (relative to your user space) address.

The only time dereferencing a pointer to zero, is when there is memory at address zero, and address zero is accessible by the application (some OSes don't allow access to various address).

For example, in some embedded systems, address zero is a vector that contains the address of the starting program. To read the value of the address vector, one has to assign a pointer with the value zero and dereference it.

Using a null pointer to access an objects methods is undefined behavior. You may be lucky if the method doesn't use any data members. If the method uses data members, the method expects those data members to be around address 0.

If you want to access object methods without creating an instance of the object, then declare the methods as static.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154