NULL指针通过“ myptr> 0&quot

发布于 2025-01-17 17:29:49 字数 175 浏览 1 评论 0原文

在某些旧代码中,我遇到了以下 NULL指针检查

if( myPtr > 0 ) {
...
}

是否有任何技术风险可以通过此IF-CHECK检查无效指针?

In some legacy code I came across the following null pointer check.

if( myPtr > 0 ) {
...
}

Are there any technical risks of checking for a null pointer via this if-check?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

—━☆沉默づ 2025-01-24 17:29:49

在C ++中,指针和整数之间的有序比较(即使整数是一个无效的指针常数,在这种情况下都是如此)。风险是允许编译器拒绝编译此类代码。


您可以将其重写为其中的任何一个:

if(myPtr != nullptr)
if(myPtr)

Ordered comparison between a pointer and an integer is ill-formed in C++ (even when the integer is a null pointer constant such as it is in this case). The risk is that compilers are allowed to, and do, refuse to compile such code.


You can rewrite it as either of these:

if(myPtr != nullptr)
if(myPtr)
回心转意 2025-01-24 17:29:49

0是一个空常数。因此,这相当于ptr> (void*)0

问题是C ++指针上的>仅适用于指针进入同一对象或数组。该规则起源于后背,当时相对疯狂的内容(例如分段内存)更为常见,但允许编译器进行优化和假设。

例如,假设px是一个数组的指针,目前指向第一个元素。然后,px> py是正确的,除非py也是指向第一个元素的指针。或者,如果px是阵列的一正式末端的指针,则px< py始终是false。

使用这种知识,编译器可以理解重写和重写循环界限,从而使编译器可以产生更快的代码。

但这也意味着px> py在一个位置而不是在另一个位置可能是正确的,因为编译器可以在一个位置证明有关py的事实,而不是另一个位置。

简而言之,请谨慎对待指针比较。

同时,NULL和任何其他指针之间的平等是很好的定义。

另外,std :: Less可以保证表现良好,并且在定义<时同意<;这允许指针的地图工作。

在这种情况下,正确的重写是:

if(myPtr!=nullptr)

如果myptr不是指针,或者

if(myPtr)

如果myptr是您的签名数字类型,则 不会编译它只是改变了行为。我会做的。

0 is a NULL constant. So this is equivalent to ptr > (void*)0.

Problem is that > on pointers in C++ is only valid for pointers into the same object or array. This rule originated from back when relatively insane stuff like segmented memory was more common, but it permits optimizations and assumptions by the compiler.

For example, suppose px is a pointer into an array, and it currently points at the first element. Then px>py is true unless py is also a pointer to the first element. Or, if px is a pointer to one-past-the-end of an array, then px<py is always false.

Using that kind of knowledge, compilers can understand refactor and rewrite loop bounds, which in turn can let the compiler produce much faster code.

But this also means that px>py could be true in one spot and not in another, because the compiler can prove a fact about py in one spot but not another.

In short, be very careful about pointer comparisons.

Meanwhile, equality between null and any other pointer is well defined.

Also std::less is guaranteed to be well behaved, and agree with < when < is defined; this permits maps of pointers to work.

In this case, the proper rewrite is:

if(myPtr!=nullptr)

which will fail to compile if myPtr isn't a pointer, or

if(myPtr)

which has the risk that if myPtr is a signed number type you just changed behaviour. I'd do the first.

恋竹姑娘 2025-01-24 17:29:49

正如其他人所说,比较无效,因此行为取决于编译器。许多OS可能将指针视为签名,因为它们将地址空间分成了一半,而负数属于内核,因此编译器可能会发布myptr&gt的签名比较。 0

有几个我看到的编译器将指针视为签名,但我现在找不到它们。但一个值得注意的例子是pointer_unsigned在Windows中,它可以解决到 __ sptr__ uptr

另请参见

As others said, the comparison is invalid so the behavior depends on compiler. It's possible that many OSes consider pointers as signed because they split the address space into halves with the negative one belongs to the kernel so compilers there may emit signed comparisons for myPtr > 0

There are a couple of cases I've seen compilers consider pointers as signed but I couldn't find them right now. But one notable example is the POINTER_SIGNED and POINTER_UNSIGNED macros in Windows which resolves to __sptr and __uptr

See also

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