473,325 Members | 2,792 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

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_cast<MyClass*>(0);
MyClass* p = static_cast<MyClass*>(0);
Thanks and best regards,
Wenjie
Jul 19 '05 #1
11 1880
On 17 Aug 2003 01:54:20 -0700, go****@yahoo.com (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?
Jul 19 '05 #2
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
Jul 19 '05 #3
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.

Jul 19 '05 #4
On Sun, 17 Aug 2003 12:26:02 +0200, Rolf Magnus <ra******@t-online.de> 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?

Jul 19 '05 #5
Dave Rahardja wrote:
On Sun, 17 Aug 2003 12:26:02 +0200, Rolf Magnus
<ra******@t-online.de> 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
Jul 19 '05 #6
On Sun, 17 Aug 2003 10:24:28 -0500, Dave Rahardja <as*@me.com> wrote:
On Sun, 17 Aug 2003 12:26:02 +0200, Rolf Magnus <ra******@t-online.de> 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.

Jul 19 '05 #7
On Sun, 17 Aug 2003 15:54:59 GMT, al***@start.no (Alf P. Steinbach) wrote:
On Sun, 17 Aug 2003 10:24:28 -0500, Dave Rahardja <as*@me.com> wrote:
On Sun, 17 Aug 2003 12:26:02 +0200, Rolf Magnus <ra******@t-online.de> 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?
}

Jul 19 '05 #8
On Sun, 17 Aug 2003 16:32:42 GMT, Dave Rahardja <as*@me.com> wrote:

MyClass* p = 0;

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


Yes.

Jul 19 '05 #9
"White Wolf" <wo***@freemail.hu> wrote in message news:<bh**********@phys-news1.kolumbus.fi>...
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
Jul 19 '05 #10
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.

Jul 19 '05 #11
Kevin Goodsell <us*********************@neverbox.com> 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
Jul 19 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: J. Campbell | last post by:
When the bitwise NOT operator, is placed in front of an integer variable type (bool, char, short, int, long), the return value is a signed int, regardless of the variable type. An example can be...
3
by: Howard | last post by:
Hi, I am maintaining a lot of code that is rife with C-style casts. I've seen a lot of comments that one should not use C-style casts at all. But I'm wondering what harm there could be in...
31
by: Jacob | last post by:
It is a common recommendation to use "static_cast<SomeType> someInstance" instead of the traditional infamous "(SomeType) someInstance". Should I use the same practice for simple types, i.e....
10
by: Ralf | last post by:
Regarding numerical types, in my view, casts fall in one of two categories: 1. Casts that change the value of an object 2. Casts that are actually redundant, but get rid of compiler/lint warnings...
24
by: roberts.noah | last post by:
Where is it in the standard that C style casts are labeled depricated? I read that on a lot of websites but I can't find it in the standard. I have BS ISO/IEC 14882:2003 (2nd ed) as published by...
9
by: liljencrantz | last post by:
Hi, I have a piece of code that uses hashtables to store pointers to various bits of data. The hashtable sees all pointers as const void *, while the application obviously uses various other...
13
by: dpbsmith.janissary.2006 | last post by:
I know C++ mostly from "learning by doing." My main reference is Stoustrup's book. I was puzzled by something in a colleague's code that looked like this: abc=BOOL(def) I asked him what that...
81
by: jacob navia | last post by:
Hi I am still writing my tutorial book about C. Here is the section about casts. I would be interested in your opinions about this. Some people have definite views about this subject ("never...
61
by: jacob navia | last post by:
Continuing the discussion about casts, I would like to know your opinions about the hairy subject of casts as lvalues, i.e. This will fail under lcc-win32, but MSVC and gcc will accept it. I...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.