473,715 Members | 6,043 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

When to use null and when to use static_cast<som e_pointer_type> (0)?

I see some code use static_cast<som e_pointer_type> (0) instead of NULL
to describe null pointer. I'm wondering what is the pros and cons of
each way. Is there any reason why we should one verses the other.

Mar 31 '06
19 3734

Thomas Tutone wrote:
What started out this subthread was your assertion that
static_cast<T*> (0) is an invalid cast.


I made so such assertion (I'm being quoted out of context), in fact I
explained why it was (go back and read my first responce, I state
clearly that that cast is valid for the same reason it is unnecissary).
What I said was that if the cast was necissary it would be an invalid
one as it would be a cast from an int to a T* and that is not a valid
static cast - such a cast would have to be a reinterpret_cas t.

Apr 4 '06 #11

Noah Roberts wrote:
Thomas Tutone wrote:
What started out this subthread was your assertion that
static_cast<T*> (0) is an invalid cast.
I made so such assertion (I'm being quoted out of context), in fact I
explained why it was (go back and read my first responce, I state
clearly that that cast is valid for the same reason it is unnecissary).


If you say so. I went back and read your first response. I don't see
where you say that the cast is valid, only where you say it is invalid.
But I don't want to argue semantics with you. You and I can agree
that it should not be necessary to cast 0 to assign it to a variable of
any pointer type.
What I said was that if the cast was necissary it would be an invalid
one as it would be a cast from an int to a T* and that is not a valid
static cast - such a cast would have to be a reinterpret_cas t.


Best regards,

Tom

Apr 4 '06 #12
Noah Roberts wrote:
Pe*******@gmail .com wrote:

template <class T>
class Nil {
public:
operator T* () { return static_cast<T*> (0); }
That cast is invalid and, luckily, unnecissary.


Later in the thread you attempt to deny that you wrote
this statement....

The above cast is definitely valid.
You can't static_cast between unrelated types. An int and a T* are
totally unrelated.
You can static_cast in any situation where there is an implicit
conversion from source to destination.

Here's another example:

struct A { };
struct B { operator A() { return A(); } };

int main() { static_cast<A>( b); }

Classes A and B are unrelated but the static_cast is good.
Luckily enough, 0 is magic in that it can be any
pointer as well as an integral.
0 cannot be a pointer. 0 is an integer.
So, that is just calling a static cast from T* to T*
It is a static cast from the integer constant 0 to a pointer to T.

There is an implicit conversion defined from integral constants
of value 0 (also known as null-pointer constants), to pointers.
...if it wasn't the code would not compile.
Nonsense, the code is fine.
What the original coder probably intended was a reinterpret cast.
He certainly did not intend that, as a reinterpret cast would not
generate a null pointer in the case where null pointers are not
all-bits-zero. I hate to think what it would do in the case where
the pointer has more bits than an integer.
However, since the static cast worked it shows that it is not
necissary to perform any casting at all.


You just said that the code would not compile without the
static cast, now you say that the cast is not necessary.

Apr 4 '06 #13
AARRRRRRRGHHHHH !

Apr 4 '06 #14

Old Wolf wrote:
You just said that the code would not compile without the
static cast, now you say that the cast is not necessary.


Now you are just making things up.

Don't go away mad, just go away.

Apr 4 '06 #15

Diego Martins wrote:
AARRRRRRRGHHHHH !


No shit, what a bunch of wankers.

Apr 4 '06 #16
Diego Martins wrote:
AARRRRRRRGHHHHH !


When you reply using Google Groups, please hit Reply -> Preview -> Edit, to
force Google to select the replied-to text. (They are optimizing their own
servers when they trick you into leaving out that text.

Then we will know who had this wonderful affect on you.

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
Apr 4 '06 #17

Phlip wrote:
Diego Martins wrote:
AARRRRRRRGHHHHH !


When you reply using Google Groups, please hit Reply -> Preview -> Edit, to
force Google to select the replied-to text. (They are optimizing their own
servers when they trick you into leaving out that text.

Then we will know who had this wonderful affect on you.


heh! it was the whole thread! you are discussing the sex of the angels
:-)

Apr 4 '06 #18

Diego Martins wrote:
Phlip wrote:
Diego Martins wrote:
AARRRRRRRGHHHHH !


When you reply using Google Groups, please hit Reply -> Preview -> Edit, to
force Google to select the replied-to text. (They are optimizing their own
servers when they trick you into leaving out that text.

Then we will know who had this wonderful affect on you.


heh! it was the whole thread! you are discussing the sex of the angels
:-)


Well, I guess it is possible that Michael is a mistranslation and
should be Michelle and I _have_ met women named Gabrielle...but I'm
still under the impression that they are masculine entities.

Apr 4 '06 #19
Phlip wrote:
Diego Martins wrote:
AARRRRRRRGHHHHH !


When you reply using Google Groups, please hit Reply -> Preview ->
Edit, to force Google to select the replied-to text. (They are
optimizing their own servers when they trick you into leaving out
that text.


The method in my .sig is one step shorter.

Brian
--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.
Apr 4 '06 #20

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

Similar topics

0
3357
by: Jiong Feng | last post by:
Hi, Here is my problem: I created a try.htm on my server, which contains a link to the default.aspx page. if I use http://localhost/try.htm, and click the link, then in default.aspx, I could get the correct HttpContext.Current.Request.UrlReferrer. but if I use https://localhost/try.htm (my server could use https), and click the link, then in default.aspx, the correct HttpContext.Current.Request.UrlReferrer is null. Is that by design...
8
2120
by: Pesso | last post by:
I'm having a difficulty compairing null to a class object whose "equal" operator is overridden.. Consider the following: class Foo { // ... public static bool operator==(Foo f1, Foo f2) { return (f1.n == f2.n); } Now the following throws:
51
10429
by: Joe Van Dyk | last post by:
When you delete a pointer, you should set it to NULL, right? Joe
4
4641
by: Richard Coltrane | last post by:
Hi there, Im stepping into C# from VB.net. In all the examples ive seen about raising events the following construct is used: if (myevent != null) myevent(this,args); Whats the purpose of the test for null? Is that testing to see if the underlying delegate is null? If so when would it be?
4
1701
by: Debbiedo | last post by:
I searched the groups and tried several approaches but still cannot find a solution. I have a table that has several hundred fields that may or may not need to be displayed in a report, depending on whether they are NULL or not. (Due to limitations of out software, the table needs to be designed this way) The data needs to be displayed like this:
2
2153
by: shankwheat | last post by:
I'm having trouble adding two values together when one of them has a null value // These two values come from a database CEOBonusValue = 550000 CEONonEqIncentCompHidden == null This should alert to true but doesn't
3
10014
by: cmartin1986 | last post by:
I have written a sql query and I need it to return 0 when it doesn't find any matches to my criteria. I have tried adding iif statements, tried sum, and just Count, all of these methods work fine to return the values when it finds matches, but i need it also to return a 0 when there are no matches. Here is what I got. SELECT "CAL Recieved" as Tags, .,sum(iif(.,1,0)) AS FROM GROUP BY . HAVING (((.)=(Date()-1))); UNION ALL SELECT...
2
1804
by: Joe | last post by:
I'm binding to a column in a TemplateField. In some cases the join I have returns a null for an int field. I would like to specify a default value somehow so I don't end up with a null exception. I'm binding the CurrentRating of the Rating control so I'm doing Convert.ToInt32(Eval('MyField')). This of course throws an exception if the value being returned is null. Is there another means of binging to that property where either a null...
9
3807
by: Francois Grieu | last post by:
When running the following code under MinGW, I get realloc(p,0) returned NULL Is that a non-conformance? TIA, Francois Grieu #include <stdio.h> #include <stdlib.h>
4
2223
by: Heikki Toivonen | last post by:
I was debugging M2Crypto function written in C which changed behavior between Python 2.6 and earlier Python versions. In an error condition the function was supposed to raise exception type A, but with 2.6 it raised type B, and further, there was no string value for the exception. I tracked this down to the C code incorrectly returning Py_None when it should have returned NULL. Changing the C code to return NULL made it behave correctly...
0
8821
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8718
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9340
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9103
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9047
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7973
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5967
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4477
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4738
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.