C++ casts on zero

Discussion in 'C++' started by Wenjie, Aug 17, 2003.

  1. Wenjie

    Wenjie Guest

    Hello,


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


    Thanks and best regards,
    Wenjie
     
    Wenjie, Aug 17, 2003
    #1
    1. Advertising

  2. On 17 Aug 2003 01:54:20 -0700, (Wenjie) wrote:

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


    That's not the problem.

    What is the problem?
     
    Alf P. Steinbach, Aug 17, 2003
    #2
    1. Advertising

  3. Wenjie

    White Wolf Guest

    Wenjie wrote:
    > Hello,
    >
    >
    > Is the following statements legal (0 is actualy
    > a pointer parameter that could be '0' and relevant
    > to the casts) ?
    > MyClass* p = reinterpret_cast<MyClass*>(0);
    > MyClass* p = static_cast<MyClass*>(0);



    What do you want to do? If you want a so-called NULL pointer, then forget
    the reinterpret_cast. 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
     
    White Wolf, Aug 17, 2003
    #3
  4. Wenjie

    Rolf Magnus Guest

    Wenjie wrote:

    > Hello,
    >
    >
    > Is the following statements legal


    Yes, though the result of the first is implementation defined.

    > (0 is actualy a pointer parameter that could be '0' and relevant
    > to the casts) ?


    I don't understand that.

    > MyClass* p = reinterpret_cast<MyClass*>(0);


    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.

    > MyClass* p = static_cast<MyClass*>(0);


    This is the same as:

    MyClass* p = 0;

    which makes p a null pointer.
     
    Rolf Magnus, Aug 17, 2003
    #4
  5. On Sun, 17 Aug 2003 12:26:02 +0200, Rolf Magnus <> wrote:

    >> MyClass* p = static_cast<MyClass*>(0);

    >
    >This is the same as:
    >
    >MyClass* p = 0;
    >
    >which makes p a null pointer.


    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?
     
    Dave Rahardja, Aug 17, 2003
    #5
  6. Wenjie

    White Wolf Guest

    Dave Rahardja wrote:
    > On Sun, 17 Aug 2003 12:26:02 +0200, Rolf Magnus
    > <> wrote:
    >
    >>> MyClass* p = static_cast<MyClass*>(0);

    >>
    >> This is the same as:
    >>
    >> MyClass* p = 0;
    >>
    >> which makes p a null pointer.

    >
    > 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?


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

    WW
     
    White Wolf, Aug 17, 2003
    #6
  7. On Sun, 17 Aug 2003 10:24:28 -0500, Dave Rahardja <> wrote:

    >On Sun, 17 Aug 2003 12:26:02 +0200, Rolf Magnus <> wrote:
    >
    >>> MyClass* p = static_cast<MyClass*>(0);

    >>
    >>This is the same as:
    >>
    >>MyClass* p = 0;
    >>
    >>which makes p a null pointer.

    >
    >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?


    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.
     
    Alf P. Steinbach, Aug 17, 2003
    #7
  8. On Sun, 17 Aug 2003 15:54:59 GMT, (Alf P. Steinbach) wrote:

    >On Sun, 17 Aug 2003 10:24:28 -0500, Dave Rahardja <> wrote:
    >
    >>On Sun, 17 Aug 2003 12:26:02 +0200, Rolf Magnus <> wrote:
    >>
    >>>> MyClass* p = static_cast<MyClass*>(0);
    >>>
    >>>This is the same as:
    >>>
    >>>MyClass* p = 0;
    >>>
    >>>which makes p a null pointer.

    >>
    >>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?

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


    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?
    }
     
    Dave Rahardja, Aug 17, 2003
    #8
  9. On Sun, 17 Aug 2003 16:32:42 GMT, Dave Rahardja <> wrote:
    >
    >MyClass* p = 0;
    >
    >if (p == 0) {
    > // Always true?
    >}
    >


    Yes.
     
    Alf P. Steinbach, Aug 17, 2003
    #9
  10. Wenjie

    Wenjie Guest

    "White Wolf" <> wrote in message news:<bhniic$d7s$>...
    > Wenjie wrote:
    > > Hello,
    > >
    > >
    > > Is the following statements legal (0 is actualy
    > > a pointer parameter that could be '0' and relevant
    > > to the casts) ?
    > > MyClass* p = reinterpret_cast<MyClass*>(0);
    > > MyClass* p = static_cast<MyClass*>(0);

    >
    >
    > What do you want to do? If you want a so-called NULL pointer, then forget
    > the reinterpret_cast. 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


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

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


    Thanks for your kind explanations.
    Wenjie
     
    Wenjie, Aug 18, 2003
    #10
  11. Wenjie wrote:
    >
    > I have a function accepting MyClass*, while the calling function did
    > a cast (reinterpret_cast) on the data to MyClass*:
    > calling_func() {


    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.

    > GenericData * p = generate_it();
    > accept_my_class(reinterpret_cast<MyClass*>(p));
    > }
    >
    > then in accept_my_class(), I would like to check the NULL pointer-ness
    > firstly.


    reinterpret_cast 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.
     
    Kevin Goodsell, Aug 18, 2003
    #11
  12. Wenjie

    Wenjie Guest

    Kevin Goodsell <> wrote in message news:<3f411ed2@shknews01>...
    > Wenjie wrote:
    > >
    > > I have a function accepting MyClass*, while the calling function did
    > > a cast (reinterpret_cast) on the data to MyClass*:
    > > calling_func() {

    >
    > 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.
    >
    > > GenericData * p = generate_it();
    > > accept_my_class(reinterpret_cast<MyClass*>(p));
    > > }
    > >
    > > then in accept_my_class(), I would like to check the NULL pointer-ness
    > > firstly.

    >
    > reinterpret_cast 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



    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
     
    Wenjie, Aug 24, 2003
    #12
    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. =?Utf-8?B?Q2hyaXMgRGF2b2xp?=

    Web casts in ASP.Net

    =?Utf-8?B?Q2hyaXMgRGF2b2xp?=, Oct 19, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    526
    clintonG
    Oct 19, 2005
  2. cgbusch
    Replies:
    2
    Views:
    348
    Sudsy
    Jul 8, 2003
  3. Zhiqiang Ye
    Replies:
    53
    Views:
    10,412
    Dan Pop
    Jun 28, 2004
  4. Gerard Flanagan
    Replies:
    3
    Views:
    494
    Terry Hancock
    Nov 19, 2005
  5. Christopher Benson-Manica

    Doubles and zero/negative zero

    Christopher Benson-Manica, Jun 30, 2004, in forum: C Programming
    Replies:
    4
    Views:
    710
    Walter
    Jul 1, 2004
Loading...

Share This Page