在C++中可以使用0L来初始化指针造成问题?
在这个问题中,初始化器用于将指针设置为空。使用 0L
值,而不是使用 0
值。我读到应该使用精确的 0
来表示空指针,因为精确的空指针表示是特定于实现的。
使用 0L
将指针设置为 null 会导致移植时出现问题吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
在 C++0x 中,有一个名为
nullptr
的新空指针常量,其类型为nullptr_t
。它可以隐式转换为任何指针类型。在那之前,我建议使用 NULL,但是 0 和 0L 都适用于我听说过的每个实现。
In C++0x there is a new null pointer constant called
nullptr
of typenullptr_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.
我无法给你一个确切的答案,但无论你的问题的确切答案是什么,用
0L
初始化指针的想法本身就是错误的,因为它表明指针是与long
的大小相同,当然这不一定是真的。无论编译器是否接受它,无论它是否安全,我都不会这样做,因为它暗示了一个无效的想法。
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 along
, 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.
空指针表示是特定于实现的,但 0 和 0L 都不一定是该实现 - 它们只是用户可以指定空指针的方式,两者都可以。
我应该指出,Bjarne Stroustrup 多次表示他更喜欢使用纯 0(而不是 NULL)。
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).
我不同意那些建议使用 NULL 的人,尽管上述三个选项中的任何一个确实都可以工作。在惯用的 C++ 中,标准选择是 0(也许是因为 Stroustrup 的偏好:http ://www2.research.att.com/~bs/bs_faq2.html#null)。
0L 将以与 0 相同的方式隐式转换为适当的指针类型;它只是看起来有点奇怪,而且对于 Jesper 给出的原因来说并没有多大意义。
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..
您可以使用
false
、0
、0l
、0L
、0ul
、0u
、00
或任何表示文字零的方法。文字零基本上是一个神奇的符号,恰好也对应于空指针常量。如果我们讨论的值恰好是按位零,并且代码通过指针对它进行 memcpy,那么我们就会遇到上面提到的问题。
附加问题:哪些类型具有非按位零的空指针常量?
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?
我必须同意每个说 NULL 的人的观点。对于那些喜欢使用 0 的人,我认为您的代码应该说明它的含义。您想要的结果是一个空指针,因此您的代码应该这么说。当你进行比较时,它就更有意义了:
比
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:
is much clearer than
将 0L(长整数(64 位))分配给 32 位指针可能会导致问题。正如在另一篇文章中提到的,使用 NULL 将指针初始化为零。
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.
根据标准(4.10.1):
,所以我猜
0L
是可以的。From the standard (4.10.1):
so I guess
0L
is ok.我认为
0L
很好,除了它暗示您认为分配给的指针类型与 long 的大小相同,因此有点令人困惑。我认为使用
NULL
更清晰。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.