C++ casts on zero

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

    C++ casts on zero

    Hello,


    Is the following statements legal (0 is actualy
    a pointer parameter that could be '0' and relevant
    to the casts) ?
    MyClass* p = reinterpret_cas t<MyClass*>(0) ;
    MyClass* p = static_cast<MyC lass*>(0);


    Thanks and best regards,
    Wenjie
  • Alf P. Steinbach

    #2
    Re: C++ casts on zero

    On 17 Aug 2003 01:54:20 -0700, gokkog@yahoo.co m (Wenjie) wrote:
    [color=blue]
    >Is the following statements legal (0 is actualy
    >a pointer parameter that could be '0' and relevant
    >to the casts) ?
    >MyClass* p = reinterpret_cas t<MyClass*>(0) ;
    >MyClass* p = static_cast<MyC lass*>(0);[/color]

    That's not the problem.

    What is the problem?


    Comment

    • White Wolf

      #3
      Re: C++ casts on zero

      Wenjie wrote:[color=blue]
      > Hello,
      >
      >
      > Is the following statements legal (0 is actualy
      > a pointer parameter that could be '0' and relevant
      > to the casts) ?
      > MyClass* p = reinterpret_cas t<MyClass*>(0) ;
      > MyClass* p = static_cast<MyC lass*>(0);[/color]


      What do you want to do? If you want a so-called NULL pointer, then forget
      the reinterpret_cas t. Why? Because that one is implementation defined.
      And a NULL pointers implementation is not necessarily a pointer having the
      same implementation as an integer containing zero. About the second cast::
      it is unecessary. The 0 itself will automagically convert to a NULL pointer
      of the right type.

      WW aka Attila


      Comment

      • Rolf Magnus

        #4
        Re: C++ casts on zero

        Wenjie wrote:
        [color=blue]
        > Hello,
        >
        >
        > Is the following statements legal[/color]

        Yes, though the result of the first is implementation defined.
        [color=blue]
        > (0 is actualy a pointer parameter that could be '0' and relevant
        > to the casts) ?[/color]

        I don't understand that.
        [color=blue]
        > MyClass* p = reinterpret_cas t<MyClass*>(0) ;[/color]

        If you wanted a null pointer, note that the above doesn't necessarily
        lead to one. It _might_ lead to a pointer with all bits zero, and that
        pointer _might_ be a null pointer, but that's not guaranteed.
        [color=blue]
        > MyClass* p = static_cast<MyC lass*>(0);[/color]

        This is the same as:

        MyClass* p = 0;

        which makes p a null pointer.

        Comment

        • Dave Rahardja

          #5
          Re: C++ casts on zero

          On Sun, 17 Aug 2003 12:26:02 +0200, Rolf Magnus <ramagnus@t-online.de> wrote:
          [color=blue][color=green]
          >> MyClass* p = static_cast<MyC lass*>(0);[/color]
          >
          >This is the same as:
          >
          >MyClass* p = 0;
          >
          >which makes p a null pointer.[/color]

          Are you saying that MyClass* p = 0 always assigns the correct NULL pointer
          value to p, even though the NULL value may not be zero on your particular
          platform?

          Comment

          • White Wolf

            #6
            Re: C++ casts on zero

            Dave Rahardja wrote:[color=blue]
            > On Sun, 17 Aug 2003 12:26:02 +0200, Rolf Magnus
            > <ramagnus@t-online.de> wrote:
            >[color=green][color=darkred]
            >>> MyClass* p = static_cast<MyC lass*>(0);[/color]
            >>
            >> This is the same as:
            >>
            >> MyClass* p = 0;
            >>
            >> which makes p a null pointer.[/color]
            >
            > Are you saying that MyClass* p = 0 always assigns the correct NULL
            > pointer value to p, even though the NULL value may not be zero on
            > your particular platform?[/color]

            Yes. As long as we speak of the C++ language. The standard requires this
            behavior.

            WW


            Comment

            • Alf P. Steinbach

              #7
              Re: C++ casts on zero

              On Sun, 17 Aug 2003 10:24:28 -0500, Dave Rahardja <ask@me.com> wrote:
              [color=blue]
              >On Sun, 17 Aug 2003 12:26:02 +0200, Rolf Magnus <ramagnus@t-online.de> wrote:
              >[color=green][color=darkred]
              >>> MyClass* p = static_cast<MyC lass*>(0);[/color]
              >>
              >>This is the same as:
              >>
              >>MyClass* p = 0;
              >>
              >>which makes p a null pointer.[/color]
              >
              >Are you saying that MyClass* p = 0 always assigns the correct NULL pointer
              >value to p, even though the NULL value may not be zero on your particular
              >platform?[/color]

              That's what he's saying: it's a requirement that the C++ standard
              places on NULL.

              Search the standard for NULL.

              Note that there's a difference between the specification "0" and
              the resulting bitpattern, which may or may not be all zeros.

              Comment

              • Dave Rahardja

                #8
                Re: C++ casts on zero

                On Sun, 17 Aug 2003 15:54:59 GMT, alfps@start.no (Alf P. Steinbach) wrote:
                [color=blue]
                >On Sun, 17 Aug 2003 10:24:28 -0500, Dave Rahardja <ask@me.com> wrote:
                >[color=green]
                >>On Sun, 17 Aug 2003 12:26:02 +0200, Rolf Magnus <ramagnus@t-online.de> wrote:
                >>[color=darkred]
                >>>> MyClass* p = static_cast<MyC lass*>(0);
                >>>
                >>>This is the same as:
                >>>
                >>>MyClass* p = 0;
                >>>
                >>>which makes p a null pointer.[/color]
                >>
                >>Are you saying that MyClass* p = 0 always assigns the correct NULL pointer
                >>value to p, even though the NULL value may not be zero on your particular
                >>platform?[/color]
                >
                >That's what he's saying: it's a requirement that the C++ standard
                >places on NULL.
                >
                >Search the standard for NULL.
                >
                >Note that there's a difference between the specification "0" and
                >the resulting bitpattern, which may or may not be all zeros.[/color]

                Wow, you learn something new every day. I've still not downloaded the standard
                (pure tightwad behavior on my part), but I should do that one of these days.

                How about comparisons...

                MyClass* p = 0;

                if (p == 0) {
                // Always true?
                }

                Comment

                • Alf P. Steinbach

                  #9
                  Re: C++ casts on zero

                  On Sun, 17 Aug 2003 16:32:42 GMT, Dave Rahardja <ask@me.com> wrote:[color=blue]
                  >
                  >MyClass* p = 0;
                  >
                  >if (p == 0) {
                  > // Always true?
                  >}
                  >[/color]

                  Yes.

                  Comment

                  • Wenjie

                    #10
                    Re: C++ casts on zero

                    "White Wolf" <wolof@freemail .hu> wrote in message news:<bhniic$d7 s$1@phys-news1.kolumbus. fi>...[color=blue]
                    > Wenjie wrote:[color=green]
                    > > Hello,
                    > >
                    > >
                    > > Is the following statements legal (0 is actualy
                    > > a pointer parameter that could be '0' and relevant
                    > > to the casts) ?
                    > > MyClass* p = reinterpret_cas t<MyClass*>(0) ;
                    > > MyClass* p = static_cast<MyC lass*>(0);[/color]
                    >
                    >
                    > What do you want to do? If you want a so-called NULL pointer, then forget
                    > the reinterpret_cas t. Why? Because that one is implementation defined.
                    > And a NULL pointers implementation is not necessarily a pointer having the
                    > same implementation as an integer containing zero. About the second cast::
                    > it is unecessary. The 0 itself will automagically convert to a NULL pointer
                    > of the right type.
                    >
                    > WW aka Attila[/color]

                    I have a function accepting MyClass*, while the calling function did
                    a cast (reinterpret_ca st) on the data to MyClass*:
                    calling_func() {
                    GenericData * p = generate_it();
                    accept_my_class (reinterpret_ca st<MyClass*>(p) );
                    }

                    then in accept_my_class (), I would like to check the NULL pointer-ness
                    firstly.


                    Thanks for your kind explanations.
                    Wenjie

                    Comment

                    • Kevin Goodsell

                      #11
                      Re: C++ casts on zero

                      Wenjie wrote:[color=blue]
                      >
                      > I have a function accepting MyClass*, while the calling function did
                      > a cast (reinterpret_ca st) on the data to MyClass*:
                      > calling_func() {[/color]

                      You need a return type for this function. ALL functions (except
                      constructors, destructors, conversion operators, and maybe some other
                      special cases I'm forgetting) must have explicit return types.
                      [color=blue]
                      > GenericData * p = generate_it();
                      > accept_my_class (reinterpret_ca st<MyClass*>(p) );
                      > }
                      >
                      > then in accept_my_class (), I would like to check the NULL pointer-ness
                      > firstly.[/color]

                      reinterpret_cas t is the Wrong Thing to do in that case. In fact, this
                      reeks of poor organization. What is the relationship between GenericData
                      and MyClass? If there is none, then you shouldn't be treating one as if
                      it is the other. If there is a relationship between the two, then it
                      should probably be modeled through inheritance, which should make any
                      cast unnecessary.

                      A cast is often an indication of a design error, and always a potential bug.

                      If you tell us what you really want to do, we can probably recommend a
                      cleaner way to do it.

                      -Kevin
                      --
                      My email address is valid, but changes periodically.
                      To contact me please use the address from a recent posting.

                      Comment

                      • Wenjie

                        #12
                        Re: C++ casts on zero

                        Kevin Goodsell <usenet1.spamfr ee.fusion@never box.com> wrote in message news:<3f411ed2@ shknews01>...[color=blue]
                        > Wenjie wrote:[color=green]
                        > >
                        > > I have a function accepting MyClass*, while the calling function did
                        > > a cast (reinterpret_ca st) on the data to MyClass*:
                        > > calling_func() {[/color]
                        >
                        > You need a return type for this function. ALL functions (except
                        > constructors, destructors, conversion operators, and maybe some other
                        > special cases I'm forgetting) must have explicit return types.
                        >[color=green]
                        > > GenericData * p = generate_it();
                        > > accept_my_class (reinterpret_ca st<MyClass*>(p) );
                        > > }
                        > >
                        > > then in accept_my_class (), I would like to check the NULL pointer-ness
                        > > firstly.[/color]
                        >
                        > reinterpret_cas t is the Wrong Thing to do in that case. In fact, this
                        > reeks of poor organization. What is the relationship between GenericData
                        > and MyClass? If there is none, then you shouldn't be treating one as if
                        > it is the other. If there is a relationship between the two, then it
                        > should probably be modeled through inheritance, which should make any
                        > cast unnecessary.
                        >
                        > A cast is often an indication of a design error, and always a potential bug.
                        >
                        > If you tell us what you really want to do, we can probably recommend a
                        > cleaner way to do it.
                        >
                        > -Kevin[/color]


                        Thank you C++ people! I am being very late on the thread.
                        The problem is perhaps discussed here several times: it is
                        about message routing and dispatching. Concretely, we
                        have the following structure:

                        ------- --------
                        | App0 | | App1 |
                        --o---- ----o---
                        | |
                        --o-----------o---
                        | Communication |
                        ------------------
                        The communiation is socket based and it only cares about
                        the so called generic message header, the message body
                        is casted to and from in App0, App1, .... For App0, App1
                        etc's entry point, there is a message dispathing entity.

                        To add a little complexity to the discussion, some of the
                        Apps (protocol layers) is/are implemented in C on different
                        HW architecture.

                        Currently our engineer who architect the development have
                        the following C++ message structure definition:

                        typedef struct {
                        GenericHeader* pHead;
                        AppxData data;
                        } GenericMesage;


                        And then the ugly casts when AppxData is needed in Appx...


                        Best regards,
                        Wenjie

                        Comment

                        Working...