• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag
 

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: New C++ warning about comparing this==NULL
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: New C++ warning about comparing this==NULL


  • Subject: Re: New C++ warning about comparing this==NULL
  • From: Roland King <email@hidden>
  • Date: Thu, 03 Dec 2015 08:20:25 +0800


On 3 Dec 2015, at 07:43, Jens Alfke <email@hidden> wrote:


On Dec 2, 2015, at 12:02 PM, Anders Montonen <email@hidden> wrote:

As I understand it, it has never been legal, since calling the method requires dereferencing a NULL pointer. From ISO/IEC 14882:2003(E)), paragraph 5.2.5/3:
“If E1 has the type 'pointer to class X,' then the _expression_ E1->E2 is converted to the equivalent form (*(E1)).E2”

That doesn’t necessarily mean it dereferences the NULL pointer. If you take the address of the above _expression_, for example, there’s no such dereference. That’s basically what happens in a non virtual method call.

If x is a value of class/struct X, and y is a non-virtual method, then x.y() turns into X::y(&x).

So putting the two together, if x is a pointer to X, then x->y() turns into X::y(x). There’s no pointer dereference involved, and no issue if x is NULL. All that happens is that in the implementation of y(), `this` will be NULL.

I wouldn’t be surprised if modern compilers removed the NULL check as impossible. See also <http://www.viva64.com/en/b/0226/>

The crux of that post is: “…the compiler can optimize the code comparing "this" pointer to null. According to the Standard, this cannot be null and therefore the checks and the corresponding code branches can be eliminated”.

Which goes back to my original question: did the C++ standard get updated to say that method calls to NULL are invalid?

—Jens

The Internet doesn’t appear to know. My last 20 minutes of reading leads me to believe the consensus is it’s undefined but the standard doesn’t say it explicitly and people have argued that it’s fine. There’s even a closed standards issue which asks that exact question and is closed with ‘it’s fine’, but it’s a very old one. (http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_closed.html#315)

Best discussion I found so far, although not entirely persuasive, was http://stackoverflow.com/questions/2474018/when-does-invoking-a-member-function-on-a-null-instance-result-in-undefined-behav. (and yes I’ve just linked a StackOverflow post, it does happen :) )

That leads to this http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#232 issue which adds wording which, to my reading, tries to make it undefined. I cannot find a specification however which actually includes that wording, or anything equivalent, and I have now exhausted my google-fu. 

This is all entirely unhelpful, the only point I’d make is that if clang is now warning for it that means it *is* assuming the pointer is non-null and if you optimise your code it can remove the check entirely. What gcc does I have no idea, but that optimiser is getting more aggressive too, so were it me, I’d hold my nose, say that it’s settled that it’s undefined and avoid doing it, much as I agree with you that I don’t see what’s really wrong with it. 
 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

References: 
 >New C++ warning about comparing this==NULL (From: Jens Alfke <email@hidden>)
 >Re: New C++ warning about comparing this==NULL (From: Anders Montonen <email@hidden>)
 >Re: New C++ warning about comparing this==NULL (From: Jens Alfke <email@hidden>)

  • Prev by Date: Re: New C++ warning about comparing this==NULL
  • Next by Date: Re: New C++ warning about comparing this==NULL
  • Previous by thread: Re: New C++ warning about comparing this==NULL
  • Next by thread: Re: New C++ warning about comparing this==NULL
  • Index(es):
    • Date
    • Thread