references as null

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

    references as null

    Hi Everyone,

    I was wondering if there is any way to have a reference initialized
    to NULL just like a pointer.

    Thanks in advance!!!
  • =?iso-8859-1?b?VG9t4XMg0yBoyWlsaWRoZQ==?=

    #2
    Re: references as null

    Rahul:
    Hi Everyone,
    >
    I was wondering if there is any way to have a reference initialized
    to NULL just like a pointer.
    >
    Thanks in advance!!!

    There is of course:

    int *p = 0;

    int &i = *p;


    Only problem with this though is that the Standard leaves the behaviour
    as explicitly undefined when you dereference a null pointer. So that
    means don't do it in a portable program unless you want the hard disk
    heads to crash and destroy all data on your disk :-D

    And as far as I know, there's no other means of getting a "reference to
    null" than dereferencing a null pointer (...I can't think of any off-hand
    anyway).

    Maybe your own system will let you do it, but it's not a habit you wanna
    pick up if you want to program portably.

    --
    Tomás Ó hÉilidhe

    Comment

    • Kira Yamato

      #3
      Re: references as null

      On 2007-12-01 07:34:51 -0500, Rahul <sam_cit@yahoo. co.insaid:
      Hi Everyone,
      >
      I was wondering if there is any way to have a reference initialized
      to NULL just like a pointer.
      I suppose you can try
      Object &x = *(Object *)0;

      And then you can test for NULL reference with
      if (&x == 0) ...

      But just because you could, should you?

      --

      -kira

      Comment

      • Rahul

        #4
        Re: references as null

        On Dec 1, 5:58 pm, Kira Yamato <kira...@earthl ink.netwrote:
        On 2007-12-01 07:34:51 -0500, Rahul <sam_...@yahoo. co.insaid:
        >
        Hi Everyone,
        >
        I was wondering if there is any way to have a reference initialized
        to NULL just like a pointer.
        >
        I suppose you can try
        Object &x = *(Object *)0;
        >
        And then you can test for NULL reference with
        if (&x == 0) ...
        >
        But just because you could, should you?
        >
        --
        >
        -kira

        I just wanted to know the possibility of passing a NULL reference to a
        copy constructor and as per your code i'm able to do so and vc++ is
        crashing :-)

        class copu
        {
        int j;
        public:
        copu(const copu& obj)
        {
        printf("in copy constructor...% d\n",j);
        j = obj.j; //-crash over
        here ;-)
        printf("in copy constructor...2 . %d\n",j);
        }
        copu()
        {
        j = 10;
        printf("in default constructor...% d\n",j);
        }

        };

        int main()
        {
        copu obj;
        copu& ref = *(copu*)0;
        copu sam = ref; //-invokes the copy constructor
        }

        Comment

        • Rahul

          #5
          Re: references as null

          On Dec 1, 6:19 pm, Rahul <sam_...@yahoo. co.inwrote:
          On Dec 1, 5:58 pm, Kira Yamato <kira...@earthl ink.netwrote:
          >
          >
          >
          On 2007-12-01 07:34:51 -0500, Rahul <sam_...@yahoo. co.insaid:
          >
          Hi Everyone,
          >
          I was wondering if there is any way to have a reference initialized
          to NULL just like a pointer.
          >
          I suppose you can try
          Object &x = *(Object *)0;
          >
          And then you can test for NULL reference with
          if (&x == 0) ...
          >
          But just because you could, should you?
          >
          --
          >
          -kira
          >
          I just wanted to know the possibility of passing a NULL reference to a
          copy constructor and as per your code i'm able to do so and vc++ is
          crashing :-)
          >
          class copu
          {
          int j;
          public:
          copu(const copu& obj)
          {
          printf("in copy constructor...% d\n",j);
          j = obj.j; //-crash over
          here ;-)
          printf("in copy constructor...2 . %d\n",j);
          }
          copu()
          {
          j = 10;
          printf("in default constructor...% d\n",j);
          }
          >
          };
          >
          int main()
          {
          copu obj;
          copu& ref = *(copu*)0;
          copu sam = ref; //-invokes the copy constructor
          >
          }
          So is there anyway to avoid referring to a variable of a NULL
          reference? A developer of a class should consider this for a robust
          class, he can't expect the user of the class to do the correct things.
          I just want to have a graceful exit from the copy constructor...

          Comment

          • yanlinlin82@gmail.com

            #6
            Re: references as null

            In my opinion, you may check it like this:

            copu::copu(cons t copu& obj)
            {
            assert(&obj != NULL);
            //...
            j = obj.j
            //...
            }

            But since the obj is invalid, the copy constructing can not be all
            right. Aborting by assert is the best. :P

            Comment

            • peter koch

              #7
              Re: references as null

              On 1 Dec., 14:22, Rahul <sam_...@yahoo. co.inwrote:
              On Dec 1, 6:19 pm, Rahul <sam_...@yahoo. co.inwrote:
              >
              >
              >
              >
              >
              On Dec 1, 5:58 pm, Kira Yamato <kira...@earthl ink.netwrote:
              >
              On 2007-12-01 07:34:51 -0500, Rahul <sam_...@yahoo. co.insaid:
              >
              Hi Everyone,
              >
              I was wondering if there is any way to have a reference initialized
              to NULL just like a pointer.
              >
              I suppose you can try
              Object &x = *(Object *)0;
              >
              And then you can test for NULL reference with
              if (&x == 0) ...
              >
              But just because you could, should you?
              >
              --
              >
              -kira
              >
              I just wanted to know the possibility of passing a NULL reference to a
              copy constructor and as per your code i'm able to do so and vc++ is
              crashing :-)
              >
              class copu
              {
              int j;
              public:
              copu(const copu& obj)
              {
              printf("in copy constructor...% d\n",j);
              j = obj.j; //-crash over
              here ;-)
              printf("in copy constructor...2 . %d\n",j);
              }
              copu()
              {
              j = 10;
              printf("in default constructor...% d\n",j);
              }
              >
              };
              >
              int main()
              {
              copu obj;
              copu& ref = *(copu*)0;
              copu sam = ref; //-invokes the copy constructor
              >
              }
              >
              So is there anyway to avoid referring to a variable of a NULL
              reference? A developer of a class should consider this for a robust
              class, he can't expect the user of the class to do the correct things.
              I just want to have a graceful exit from the copy constructor...
              The problem with the code above is that you invoke undefined behaviour
              by dereferencing a null pointer. This code is not worth bothering
              about (the program becomes invalid at that point), so there is no
              reason and no need to check for this.

              /Peter

              Comment

              • Rolf Magnus

                #8
                Re: references as null

                Rahul wrote:
                So is there anyway to avoid referring to a variable of a NULL
                reference?
                Yes. Don't initialize a reference by dereferencing a null pointer. The place
                where the error happens (and where the C++ standard says that the behavior
                becomes undefined) is the place where you do that, not the place where you
                use the invalid reference.
                A developer of a class should consider this for a robust class, he can't
                expect the user of the class to do the correct things.
                He must. There is always a way to screw things up. It is more likely that
                the user provides a reference to an object that is already destroyed. That
                would be just as disastrous, and there is no way at all to check for that.
                I just want to have a graceful exit from the copy constructor...
                By doing what?

                Comment

                • Ron Natalie

                  #9
                  Re: references as null

                  Rahul wrote:
                  Hi Everyone,
                  >
                  I was wondering if there is any way to have a reference initialized
                  to NULL just like a pointer.
                  >
                  Thanks in advance!!!
                  No, there's no such thing as a null reference and anything
                  that coerces one almost certainly causes undefined behavior.

                  One thing you can do is use a reference to an object that you
                  can tell from a valid one:

                  class C {
                  public:
                  static C my_null;

                  bool IsNull(const C& t) {
                  return &t == &my_null;
                  }

                  void SomeFunc(const C& c = my_null) {
                  if(IsNull(c)) {...

                  Comment

                  • James Kanze

                    #10
                    Re: references as null

                    On Dec 1, 1:34 pm, Rahul <sam_...@yahoo. co.inwrote:
                    I was wondering if there is any way to have a reference initialized
                    to NULL just like a pointer.
                    Not legally, unless your compiler supports it as an extension.
                    (I don't know of any that do.) It's undefined behavior;
                    anything can happen.

                    --
                    James Kanze (GABI Software) email:james.kan ze@gmail.com
                    Conseils en informatique orientée objet/
                    Beratung in objektorientier ter Datenverarbeitu ng
                    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

                    Comment

                    Working...