Null Reference

Discussion in 'C++' started by JKop, Oct 1, 2004.

  1. JKop

    JKop Guest

    Does that Standard explicitly forbid the initiation of a null reference? Is
    there anything wrong with the following code?:

    void Blah( std::string const &k )
    {
    if ( !&k ) return;

    // work with k
    }

    int main()
    {
    Blah( *static_cast<std::string* const>( 0 ) );
    }

    (Not sure if reinterpret_cast is required there...)

    -JKop
    JKop, Oct 1, 2004
    #1
    1. Advertising

  2. JKop

    Phlip Guest

    JKop wrote:

    > Does that Standard explicitly forbid the initiation of a null reference?

    Is
    > there anything wrong with the following code?:
    >
    > void Blah( std::string const &k )
    > {
    > if ( !&k ) return;
    >
    > // work with k
    > }
    >
    > int main()
    > {
    > Blah( *static_cast<std::string* const>( 0 ) );


    The undefined behavior begins with the first * on that line. Dereferencing a
    pointer to NULL is undefined.

    Does anyone know a compiler that won't generate the obvious opcodes for that
    situation, and evaluate 'if ( !&k ) return;' the way anyone would expect?

    --
    Phlip
    http://industrialxp.org/community/bin/view/Main/TestFirstUserInterfaces
    Phlip, Oct 1, 2004
    #2
    1. Advertising

  3. JKop

    Sharad Kala Guest

    "JKop" <> wrote in message

    > Does that Standard explicitly forbid the initiation of a null reference?

    Is
    > there anything wrong with the following code?:


    Yes, section 8.3.2 paragraph 4

    "A reference shall be initialized to refer to a valid object or function.
    [Note: in particular, a null reference cannot exist in a well-defined
    program, because the only way to create such a reference would be to bind it
    to the ``object'' obtained by dereferencing a null pointer, which causes
    undefined behavior."

    Sharad
    Sharad Kala, Oct 1, 2004
    #3
  4. JKop

    Chris Theis Guest

    "JKop" <> wrote in message
    news:pHa7d.32562$...
    >
    > Does that Standard explicitly forbid the initiation of a null reference?

    [SNIP]

    Yes. It´s in the nature of references that there is no null reference!

    Chris
    Chris Theis, Oct 1, 2004
    #4
  5. You cannot have null references in a well defined program. The only way to
    obtain a null reference would be to deference a null pointer(as in your
    code), which results in undefined behaviour.


    "JKop" <> wrote in message
    news:pHa7d.32562$...
    >
    > Does that Standard explicitly forbid the initiation of a null reference?
    > Is
    > there anything wrong with the following code?:
    >
    > void Blah( std::string const &k )
    > {
    > if ( !&k ) return;
    >
    > // work with k
    > }
    >
    > int main()
    > {
    > Blah( *static_cast<std::string* const>( 0 ) );
    > }
    >
    > (Not sure if reinterpret_cast is required there...)
    >
    > -JKop
    Tim Threlfall, Oct 1, 2004
    #5
  6. JKop wrote:
    > Does that Standard explicitly forbid the initiation of a null reference?


    Forbid or not, the Standard does not give you means to generate one.

    - J.
    Jacek Dziedzic, Oct 1, 2004
    #6
  7. JKop

    JKop Guest

    Jacek Dziedzic posted:

    > JKop wrote:
    >> Does that Standard explicitly forbid the initiation of a null
    >> reference?

    >
    > Forbid or not, the Standard does not give you means to generate one.
    >
    > - J.
    >


    struct Blah1
    {
    int& a;
    };

    struct Blah2
    {
    int* p_a;
    };


    #include <iostream>

    int main()
    {
    Blah2 poo2 = { 0 };

    Blah1 poo1( *reinterpret_cast<Blah1 * const>(&poo2) );

    int& nulref = poo1.a;

    std::cout << "I'm a reference, and my address is: " << &nulref << '\n';
    }



    -JKop
    JKop, Oct 2, 2004
    #7
  8. JKop

    Chris Theis Guest

    "JKop" <> schrieb im Newsbeitrag
    news:nol7d.32607$...
    > Jacek Dziedzic posted:
    >
    > > JKop wrote:
    > >> Does that Standard explicitly forbid the initiation of a null
    > >> reference?

    > >
    > > Forbid or not, the Standard does not give you means to generate one.
    > >
    > > - J.
    > >

    >
    > struct Blah1
    > {
    > int& a;
    > };
    >
    > struct Blah2
    > {
    > int* p_a;
    > };
    >
    >
    > #include <iostream>
    >
    > int main()
    > {
    > Blah2 poo2 = { 0 };
    >
    > Blah1 poo1( *reinterpret_cast<Blah1 * const>(&poo2) );
    >
    > int& nulref = poo1.a;
    >
    > std::cout << "I'm a reference, and my address is: " << &nulref <<

    '\n';
    > }
    >
    > -JKop


    And what exactly are you trying to proove with this? I mean we all know that
    with the help of reinterpret_cast you can do a lot of (evil) things and it
    is (or rather should be) common knowledge that not all syntactically legal
    implementations result in well-defined programs. Your code does not show a
    way to initialize a null reference but rather how to bind a reference to a
    null pointer.

    Check out ISO/IEC 14882:2003(E) chapter 8.3.2:
    A reference shall be initialized to refer to a valid object or function.
    [Note: in particular, a null reference cannot exist in a well-defined
    program, because the only way to create such a reference would be to bind it
    to the "object" obtained by dereferencing a null pointer,
    which causes undefined behavior.

    Regards
    Chris
    Chris Theis, Oct 2, 2004
    #8
  9. JKop

    JKop Guest

    > Your code does not show a way to initialize a null reference but rather
    > how to bind a reference to a null pointer.


    Incorrect.

    My code depends upon the "Blah1" and the "Blah2" structures being identical.
    I'm depending upon hidden pointers being used in the background for
    references.

    But... the Standard doesn't make any such guarantee that references will be
    represented by hidden pointers at all! As such my code exhibits undefined
    behaviour.

    That said, I don't know of *any* system that wouldn't use hidden pointers
    for my "Blah1" structure. So... it works on all the systems I know.

    So when I make poo1 = poo2, what happens is that the hidden pointer in the
    Blah1 structure gets the value 0, ie. a null pointer. By this I have
    achieved a null reference, but without using the dereference operator.

    > Check out ISO/IEC 14882:2003(E) chapter 8.3.2:
    > A reference shall be initialized to refer to a valid object or
    > function. [Note: in particular, a null reference cannot exist in a
    > well-defined program, because the only way to create such a reference
    > would be to bind it to the "object" obtained by dereferencing a null
    > pointer, which causes undefined behavior.


    I haven't dereferenced a null pointer.


    -JKop
    JKop, Oct 2, 2004
    #9
  10. JKop

    Chris Theis Guest

    "JKop" <> wrote in message
    news:R1x7d.32623$...
    > > Your code does not show a way to initialize a null reference but rather
    > > how to bind a reference to a null pointer.

    >
    > Incorrect.
    >
    > My code depends upon the "Blah1" and the "Blah2" structures being

    identical.
    > I'm depending upon hidden pointers being used in the background for
    > references.
    >
    > But... the Standard doesn't make any such guarantee that references will

    be
    > represented by hidden pointers at all! As such my code exhibits undefined
    > behaviour.
    >
    > That said, I don't know of *any* system that wouldn't use hidden pointers
    > for my "Blah1" structure. So... it works on all the systems I know.
    >
    > So when I make poo1 = poo2, what happens is that the hidden pointer in the
    > Blah1 structure gets the value 0, ie. a null pointer. By this I have
    > achieved a null reference, but without using the dereference operator.
    >
    > > Check out ISO/IEC 14882:2003(E) chapter 8.3.2:
    > > A reference shall be initialized to refer to a valid object or
    > > function. [Note: in particular, a null reference cannot exist in a
    > > well-defined program, because the only way to create such a reference
    > > would be to bind it to the "object" obtained by dereferencing a null
    > > pointer, which causes undefined behavior.

    >
    > I haven't dereferenced a null pointer.
    >


    After rechecking your code I saw that you really do not dereference a null
    pointer but get around that by the (IMHO dubious) invocation of the copy
    ctor & reinterpret_cast. However, you still do not acutally intialize a
    null-reference but rather trick the compiler into it by this construct.

    As an answer to your original question the standard says: "Note: in
    particular, a null reference cannot exist in a
    well-defined program.".

    To make a long story short, what is your point of doing or wanting this? In
    my experience it´s much more fruitful (in the monetary sense and also
    thinking of ones career) to write well-defined programs ;-)


    Cheers
    Chris
    Chris Theis, Oct 2, 2004
    #10
  11. JKop

    JKop Guest


    > After rechecking your code I saw that you really do not dereference a
    > null pointer but get around that by the (IMHO dubious) invocation of
    > the copy ctor & reinterpret_cast. However, you still do not acutally
    > intialize a null-reference but rather trick the compiler into it by
    > this construct.


    The ends justifies the means.

    > As an answer to your original question the standard says: "Note: in
    > particular, a null reference cannot exist in a
    > well-defined program.".
    >
    > To make a long story short, what is your point of doing or wanting
    > this? In my experience it´s much more fruitful (in the monetary sense
    > and also thinking of ones career) to write well-defined programs ;-)
    >
    >
    > Cheers
    > Chris



    Is love well-defined? hehe

    What ever happened to just writing a program that didn't exhibit undefined
    behaviour? "defined programs" if you will!


    -JKop
    JKop, Oct 2, 2004
    #11
  12. "Chris Theis" <> wrote in message
    news:cjmfq6$hq8$...
    >
    > "JKop" <> wrote in message
    > news:R1x7d.32623$...
    > > > Your code does not show a way to initialize a null reference but

    rather
    > > > how to bind a reference to a null pointer.

    > >
    > > Incorrect.
    > >
    > > My code depends upon the "Blah1" and the "Blah2" structures being

    > identical.
    > > I'm depending upon hidden pointers being used in the background for
    > > references.
    > >
    > > But... the Standard doesn't make any such guarantee that references will

    > be
    > > represented by hidden pointers at all! As such my code exhibits

    undefined
    > > behaviour.
    > >
    > > That said, I don't know of *any* system that wouldn't use hidden

    pointers
    > > for my "Blah1" structure. So... it works on all the systems I know.
    > >
    > > So when I make poo1 = poo2, what happens is that the hidden pointer in

    the
    > > Blah1 structure gets the value 0, ie. a null pointer. By this I have
    > > achieved a null reference, but without using the dereference operator.
    > >
    > > > Check out ISO/IEC 14882:2003(E) chapter 8.3.2:
    > > > A reference shall be initialized to refer to a valid object or
    > > > function. [Note: in particular, a null reference cannot exist in a
    > > > well-defined program, because the only way to create such a reference
    > > > would be to bind it to the "object" obtained by dereferencing a null
    > > > pointer, which causes undefined behavior.

    > >
    > > I haven't dereferenced a null pointer.
    > >

    >
    > After rechecking your code I saw that you really do not dereference a null
    > pointer but get around that by the (IMHO dubious) invocation of the copy
    > ctor & reinterpret_cast. However, you still do not acutally intialize a
    > null-reference but rather trick the compiler into it by this construct.
    >
    > As an answer to your original question the standard says: "Note: in
    > particular, a null reference cannot exist in a
    > well-defined program.".
    >
    > To make a long story short, what is your point of doing or wanting this?

    In
    > my experience it´s much more fruitful (in the monetary sense and also
    > thinking of ones career) to write well-defined programs ;-)


    Well, if the boss ever asks for a programmer that codes UB code that appears
    to work but might fail, and enjoys doing it, we know who to refer!!
    --
    Gary
    Gary Labowitz, Oct 2, 2004
    #12
  13. JKop

    JKop Guest


    > Well, if the boss ever asks for a programmer that codes UB code that
    > appears to work but might fail, and enjoys doing it, we know who to
    > refer!!



    I take that as a compliment!


    -JKop
    JKop, Oct 2, 2004
    #13
  14. JKop

    Phlip Guest

    JKop wrote:

    > struct Blah1
    > {
    > int& a;
    > };


    This won't compile. If you fix it to compile, then it might dereference a
    NULL pointer. But...

    > int main()
    > {
    > Blah2 poo2 = { 0 };
    >
    > Blah1 poo1( *reinterpret_cast<Blah1 * const>(&poo2) );


    Undefined behavior would begin here, when you use the storage for poo2 as
    something that it is not.

    The remaining code is therefor undefined; it neither does nor does not
    "dereference a NULL pointer".

    (Great to have a target around to practice language law on, huh?)

    http://www.politicsforum.org/images/flame_warriors/flame_67.php

    --
    Phlip
    http://industrialxp.org/community/bin/view/Main/TestFirstUserInterfaces
    Phlip, Oct 2, 2004
    #14
  15. JKop

    Chris Theis Guest

    "JKop" <> schrieb im Newsbeitrag
    news:7RC7d.32657$...
    >
    > > Well, if the boss ever asks for a programmer that codes UB code that
    > > appears to work but might fail, and enjoys doing it, we know who to
    > > refer!!

    >
    >
    > I take that as a compliment!
    >
    >
    > -JKop


    There you go ;-) I'll come back to that ;-)

    Chris
    Chris Theis, Oct 2, 2004
    #15
  16. JKop

    JKop Guest

    Phlip posted:

    > JKop wrote:
    >
    >> struct Blah1
    >> {
    >> int& a;
    >> };

    >
    > This won't compile. If you fix it to compile, then it might dereference a
    > NULL pointer. But...



    Incorrect to the former. "Wha... ?" to the latter.

    >> int main()
    >> {
    >> Blah2 poo2 = { 0 };
    >>
    >> Blah1 poo1( *reinterpret_cast<Blah1 * const>(&poo2) );

    >
    > Undefined behavior would begin here, when you use the storage for poo2 as
    > something that it is not.



    I've already covered this.


    > The remaining code is therefor undefined; it neither does nor does not
    > "dereference a NULL pointer".



    Here's a medal.


    -JKop
    JKop, Oct 3, 2004
    #16
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kivak Wolf
    Replies:
    2
    Views:
    10,059
    Kivak Wolf
    Jun 28, 2005
  2. vizlab
    Replies:
    3
    Views:
    4,176
    Michael Bar-Sinai
    Oct 17, 2007
  3. Replies:
    16
    Views:
    7,289
    Mike Schilling
    Oct 12, 2005
  4. Replies:
    5
    Views:
    26,500
    Mike Schilling
    Mar 29, 2006
  5. Bo Peng
    Replies:
    13
    Views:
    13,269
    Siemel Naran
    Jul 18, 2004
Loading...

Share This Page