Question

In this question an initializer is used to set a pointer to null. Instead of using value of 0 value of 0L is used. I've read that one should use exactly 0 for null pointers because exact null pointer representation is implementation-specific.

Can using 0L to set a pointer to null cause problems while porting?

Was it helpful?

Solution

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.

OTHER TIPS

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.

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.

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.

The null pointer representation is implementation specific, but neither 0 nor 0L is necessarily that implementation - they are just ways that the user can specify the null pointer, and either is OK.

I should point out that Bjarne Stroustrup has several times indicated that his preference is to use plain 0 (and not NULL).

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

You can use false, 0, 0l, 0L, 0ul, 0u, 00, or any number of ways to represent the literal zero. The literal zero is basically a magical symbol that happens to also correspond to a null pointer constant.

If we were talking about a value that happens to be bitwise zero and the code were memcpy-ing it over the pointer, then we would have the problems mentioned above.

Bonus question: What types have a null pointer constant that isn't bitwise zero?

I would have to agree with everyone that said NULL. To those that prefer using 0, my opinion is that your code should say what it means. The result you want is a null pointer, so your code should say that. It makes even more sense when you do a comparison:

if (some_variable == NULL) { ... }

is much clearer than

if (some_variable == 0) { ... }

Assignment of 0L, a long integer (64bit) to an 32bit pointer might cause problems. As in the other post mentioned, use NULL to initializse pointers to zero.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top