在C++中可以使用0L来初始化指针造成问题?

发布于 2024-08-15 12:19:57 字数 278 浏览 6 评论 0原文

这个问题中,初始化器用于将指针设置为空。使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

嘿嘿嘿 2024-08-22 12:19:58

在 C++0x 中,有一个名为 nullptr 的新空指针常量,其类型为 nullptr_t。它可以隐式转换为任何指针类型。

在那之前,我建议使用 NULL,但是 0 和 0L 都适用于我听说过的每个实现。

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.

再见回来 2024-08-22 12:19:58

我无法给你一个确切的答案,但无论你的问题的确切答案是什么,用 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 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.

我的痛♀有谁懂 2024-08-22 12:19:58

空指针表示是特定于实现的,但 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).

爱,才寂寞 2024-08-22 12:19:58

我不同意那些建议使用 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..

骄兵必败 2024-08-22 12:19:58

您可以使用 false00l0L0ul0u00 或任何表示文字零的方法。文字零基本上是一个神奇的符号,恰好也对应于空指针常量。

如果我们讨论的值恰好是按位零,并且代码通过指针对它进行 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?

孤檠 2024-08-22 12:19:58

我必须同意每个说 NULL 的人的观点。对于那些喜欢使用 0 的人,我认为您的代码应该说明它的含义。您想要的结果是一个空指针,因此您的代码应该这么说。当你进行比较时,它就更有意义了:

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

if (some_variable == 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:

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

is much clearer than

if (some_variable == 0) { ... }
傲影 2024-08-22 12:19:58

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

一江春梦 2024-08-22 12:19:57

根据标准(4.10.1):

空指针常量是一个整数
常量表达式 (5.19) 的右值
计算结果为零的整数类型

,所以我猜 0L 是可以的。

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.

水波映月 2024-08-22 12:19:57

我认为 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文