Null Reference

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • JKop

    #1

    Null Reference


    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<st d::string* const>( 0 ) );
    }

    (Not sure if reinterpret_cas t is required there...)

    -JKop
  • Phlip

    #2
    Re: Null Reference

    JKop wrote:
    [color=blue]
    > Does that Standard explicitly forbid the initiation of a null reference?[/color]
    Is[color=blue]
    > there anything wrong with the following code?:
    >
    > void Blah( std::string const &k )
    > {
    > if ( !&k ) return;
    >
    > // work with k
    > }
    >
    > int main()
    > {
    > Blah( *static_cast<st d::string* const>( 0 ) );[/color]

    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



    Comment

    • Sharad Kala

      #3
      Re: Null Reference


      "JKop" <NULL@NULL.NULL > wrote in message
      [color=blue]
      > Does that Standard explicitly forbid the initiation of a null reference?[/color]
      Is[color=blue]
      > there anything wrong with the following code?:[/color]

      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



      Comment

      • Tim Threlfall

        #4
        Re: Null Reference

        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" <NULL@NULL.NULL > wrote in message
        news:pHa7d.3256 2$Z14.11725@new s.indigo.ie...[color=blue]
        >
        > 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<st d::string* const>( 0 ) );
        > }
        >
        > (Not sure if reinterpret_cas t is required there...)
        >
        > -JKop[/color]


        Comment

        • Chris Theis

          #5
          Re: Null Reference


          "JKop" <NULL@NULL.NULL > wrote in message
          news:pHa7d.3256 2$Z14.11725@new s.indigo.ie...[color=blue]
          >
          > Does that Standard explicitly forbid the initiation of a null reference?[/color]
          [SNIP]

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

          Chris


          Comment

          • Jacek Dziedzic

            #6
            Re: Null Reference

            JKop wrote:[color=blue]
            > Does that Standard explicitly forbid the initiation of a null reference?[/color]

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

            - J.

            Comment

            • JKop

              #7
              Re: Null Reference

              Jacek Dziedzic posted:
              [color=blue]
              > JKop wrote:[color=green]
              >> Does that Standard explicitly forbid the initiation of a null
              >> reference?[/color]
              >
              > Forbid or not, the Standard does not give you means to generate one.
              >
              > - J.
              >[/color]

              struct Blah1
              {
              int& a;
              };

              struct Blah2
              {
              int* p_a;
              };


              #include <iostream>

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

              Blah1 poo1( *reinterpret_ca st<Blah1 * const>(&poo2) );

              int& nulref = poo1.a;

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



              -JKop

              Comment

              • Chris Theis

                #8
                Re: Null Reference


                "JKop" <NULL@NULL.NULL > schrieb im Newsbeitrag
                news:nol7d.3260 7$Z14.11886@new s.indigo.ie...[color=blue]
                > Jacek Dziedzic posted:
                >[color=green]
                > > JKop wrote:[color=darkred]
                > >> Does that Standard explicitly forbid the initiation of a null
                > >> reference?[/color]
                > >
                > > Forbid or not, the Standard does not give you means to generate one.
                > >
                > > - J.
                > >[/color]
                >
                > struct Blah1
                > {
                > int& a;
                > };
                >
                > struct Blah2
                > {
                > int* p_a;
                > };
                >
                >
                > #include <iostream>
                >
                > int main()
                > {
                > Blah2 poo2 = { 0 };
                >
                > Blah1 poo1( *reinterpret_ca st<Blah1 * const>(&poo2) );
                >
                > int& nulref = poo1.a;
                >
                > std::cout << "I'm a reference, and my address is: " << &nulref <<[/color]
                '\n';[color=blue]
                > }
                >
                > -JKop[/color]

                And what exactly are you trying to proove with this? I mean we all know that
                with the help of reinterpret_cas t 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


                Comment

                • JKop

                  #9
                  Re: Null Reference

                  > Your code does not show a way to initialize a null reference but rather[color=blue]
                  > how to bind a reference to a null pointer.[/color]

                  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.
                  [color=blue]
                  > 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.[/color]

                  I haven't dereferenced a null pointer.


                  -JKop

                  Comment

                  • Chris Theis

                    #10
                    Re: Null Reference


                    "JKop" <NULL@NULL.NULL > wrote in message
                    news:R1x7d.3262 3$Z14.11852@new s.indigo.ie...[color=blue][color=green]
                    > > Your code does not show a way to initialize a null reference but rather
                    > > how to bind a reference to a null pointer.[/color]
                    >
                    > Incorrect.
                    >
                    > My code depends upon the "Blah1" and the "Blah2" structures being[/color]
                    identical.[color=blue]
                    > 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[/color]
                    be[color=blue]
                    > 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.
                    >[color=green]
                    > > 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.[/color]
                    >
                    > I haven't dereferenced a null pointer.
                    >[/color]

                    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_cas t. 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



                    Comment

                    • JKop

                      #11
                      Re: Null Reference

                      [color=blue]
                      > 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_cas t. However, you still do not acutally
                      > intialize a null-reference but rather trick the compiler into it by
                      > this construct.[/color]

                      The ends justifies the means.
                      [color=blue]
                      > 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[/color]


                      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

                      Comment

                      • Gary Labowitz

                        #12
                        Re: Null Reference

                        "Chris Theis" <Chris.Theis@no spam.cern.ch> wrote in message
                        news:cjmfq6$hq8 $1@sunnews.cern .ch...[color=blue]
                        >
                        > "JKop" <NULL@NULL.NULL > wrote in message
                        > news:R1x7d.3262 3$Z14.11852@new s.indigo.ie...[color=green][color=darkred]
                        > > > Your code does not show a way to initialize a null reference but[/color][/color][/color]
                        rather[color=blue][color=green][color=darkred]
                        > > > how to bind a reference to a null pointer.[/color]
                        > >
                        > > Incorrect.
                        > >
                        > > My code depends upon the "Blah1" and the "Blah2" structures being[/color]
                        > identical.[color=green]
                        > > 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[/color]
                        > be[color=green]
                        > > represented by hidden pointers at all! As such my code exhibits[/color][/color]
                        undefined[color=blue][color=green]
                        > > behaviour.
                        > >
                        > > That said, I don't know of *any* system that wouldn't use hidden[/color][/color]
                        pointers[color=blue][color=green]
                        > > 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[/color][/color]
                        the[color=blue][color=green]
                        > > Blah1 structure gets the value 0, ie. a null pointer. By this I have
                        > > achieved a null reference, but without using the dereference operator.
                        > >[color=darkred]
                        > > > 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.[/color]
                        > >
                        > > I haven't dereferenced a null pointer.
                        > >[/color]
                        >
                        > 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_cas t. 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?[/color]
                        In[color=blue]
                        > my experience it´s much more fruitful (in the monetary sense and also
                        > thinking of ones career) to write well-defined programs ;-)[/color]

                        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


                        Comment

                        • JKop

                          #13
                          Re: Null Reference

                          [color=blue]
                          > 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!![/color]


                          I take that as a compliment!


                          -JKop

                          Comment

                          • Phlip

                            #14
                            Re: Null Reference

                            JKop wrote:
                            [color=blue]
                            > struct Blah1
                            > {
                            > int& a;
                            > };[/color]

                            This won't compile. If you fix it to compile, then it might dereference a
                            NULL pointer. But...
                            [color=blue]
                            > int main()
                            > {
                            > Blah2 poo2 = { 0 };
                            >
                            > Blah1 poo1( *reinterpret_ca st<Blah1 * const>(&poo2) );[/color]

                            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
                            "dereferenc e a NULL pointer".

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

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

                            --
                            Phlip



                            Comment

                            • Chris Theis

                              #15
                              Re: Null Reference


                              "JKop" <NULL@NULL.NULL > schrieb im Newsbeitrag
                              news:7RC7d.3265 7$Z14.11900@new s.indigo.ie...[color=blue]
                              >[color=green]
                              > > 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!![/color]
                              >
                              >
                              > I take that as a compliment!
                              >
                              >
                              > -JKop[/color]

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

                              Chris


                              Comment

                              Working...