Can using 0L to initialize a pointer in C++ cause problems?
Solution 1
From the standard (4.10.1):
A null pointer constant is an integral constant expression (5.19) rvalue of integer type that evaluates to zero
so I guess 0L
is ok.
Solution 2
I think 0L
is fine, except for the fact that it would imply that you believe the pointer type your assigning to is the same size as a long, and so is a bit confusing.
I think using NULL
is much more clear.
Solution 3
In C++0x there is a new null pointer constant called nullptr
of type nullptr_t
. It can be implicitly converted to any pointer type.
Until then I would recommend using NULL, however 0 and 0L will both work on every implementation I've ever heard of.
Solution 4
I can't give you an exact answer, but regardless of what the exact answer to your question is, just the idea itself of initializing a pointer with 0L
is wrong, because it suggests that a pointer is the same size as a long
, which is not necessarily true, ofcourse.
Whether the compiler accepts it or not, and whether it's safe or not, I wouldn't do it because it suggests an invalid idea.
Solution 5
I would disagree with those who recommend using NULL, though any of the three stated options would indeed work. In idiomatic C++, the standard choice is 0 (perhaps because that's Stroustrup's preference: http://www2.research.att.com/~bs/bs_faq2.html#null).
0L will be implicitly cast to the appropriate pointer type in the same way as 0 would be; it just looks a bit strange and doesn't really make a great deal of sense for the reason Jesper gave..
sharptooth
Updated on June 24, 2022Comments
-
sharptooth almost 2 years
In this question an initializer is used to set a pointer to null. Instead of using value of
0
value of0L
is used. I've read that one should use exactly0
for null pointers because exact null pointer representation is implementation-specific.Can using
0L
to set a pointer to null cause problems while porting? -
Admin over 14 yearsDoes that mean that initialising it with 0 suggests it is the same size as an int? I don't think so.
-
wheaties over 14 years+1 because NULL is more clear to those maintaining the software. Trust me, I've seen deadbeef and 0 used in the same project.
-
dicroce over 14 yearsNo, because 0 does not imply an int.
-
KernelJ over 14 years0xDEADBEEF is especially bad. 0 is the only value you can always set a pointer to be (a null pointer), and it doesn't matter what size the pointer is.
-
David Rodríguez - dribeas over 14 yearsWhat problems do you refer to? AFAIK, the compiler will perform the required type conversions.
-
KernelJ over 14 yearsThe value 0 can be cast to any size of integer from any other.
-
Jesper over 14 years@Neil: Even if it does, it's less bad than explicitly saying
0L
, because theL
strongly suggests that it's supposed to be understood as along
. Just0
doesn't necessarily by itself imply that it's anint
. -
David Rodríguez - dribeas over 14 yearsI do prefer 0 also, but that is just a matter of taste, I guess.
-
KernelJ over 14 yearsI think the fact that you're explicitly saying the value is a long is the problem here.
-
McPherrinM over 14 years0 is defined to work in the C++ standard. NULL is just a macro carried over from C.
-
Brian R. Bondy over 14 yearsdefined as 0, still I have a preference to NULL as I personally think it looks cleaner.
-
David Thornley over 14 yearsNULL is legal and idiomatic. 0 is legal and idiomatic. 0L is legal but not idiomatic. 0xDEADBEEF is either unspecified or undefined, and certainly isn't a null pointer constant. I'm looking forward to
nullptr
myself. -
josesuero over 14 yearsYou are not assigning "a long integer", but "the null pointer constant". And that is guaranteed to work, regardless of the bit width of integers or pointers.
-
KernelJ over 14 yearsSeven characters for something which is basically a 0 is asking a bit much I think. I can't think why so much verbosity is required, when we have NULL already. Maybe it has some special uses in the new standard but I haven't really looked at it.
-
Jesper over 14 years@KernelJ: Yes, that's exactly what I mean.
-
MSalters over 14 yearsOverload resolution. Given
f(int)
andf(int*)
,f(NULL)
callsf(int)
butf(nullptr)
callsf(int*)
. -
MSalters over 14 yearsI personally prefer
if (!some_variable) { }
-
codencandy over 14 yearsAll of you are right, my thinking was that assignment of 64 bit integers to a 32 bit pointer would be invalid. but as you mentioned the compiler will convert the constant to the required type.
-
KernelJ over 14 yearsYeah, that's great but... Anyone who overloads functions like that needs to be shot.
-
Bill over 14 yearsNote: Stroustrup recommends using nullptr when that's available, so he's not recommending 0 so much as recommending against using a macro.
-
dan04 over 13 years@KernelJ:
print(int)
andprint(char*)
is a perfectly reasonable overload.