• 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: Jens Alfke <email@hidden>
  • Date: Wed, 02 Dec 2015 23:03:37 -0800


On Dec 2, 2015, at 10:41 PM, Clark Cox <email@hidden> wrote:

Widget *w = factory.getCurrentWidget();
Paint *p = w->getPaint();
If the currentWidget is NULL, the second line does this.

That counts as a pointer dereference (the special case of &*foo is just that, a special case).

It syntactically looks like one, but it isn’t one. “Dereference” means referencing, i.e. accessing, the memory location pointed to. A nonvirtual method call doesn’t do that. 

The reason it’s undefined in the standard to dereference NULL is because on almost all operating systems that’s unmapped memory and will cause a crash. It isn’t just because of squeamishness about what the LHS of a -> operator should be.


On Dec 2, 2015, at 10:22 PM, Marco S Hyman <email@hidden> wrote:

Isn’t it a compiler implementation detail that w->getPaint under the covers turns into getPaint(w)?

In many languages I’d agree with you. But C++ has a very explicit design principle of not adding extra overhead unless you ask for it. If you want the more powerful (but slower) behavior of virtual methods (inheritance, etc.) you have to explicitly say “virtual”; otherwise a method call is implemented just like a regular C function call.

—Jens
 _______________________________________________
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

  • Follow-Ups:
    • Re: New C++ warning about comparing this==NULL
      • From: Doug Hill <email@hidden>
    • Re: New C++ warning about comparing this==NULL
      • From: Clark Cox <email@hidden>
References: 
 >New C++ warning about comparing this==NULL (From: Jens Alfke <email@hidden>)
 >Re: New C++ warning about comparing this==NULL (From: Jonathan Prescott <email@hidden>)
 >Re: New C++ warning about comparing this==NULL (From: Jens Alfke <email@hidden>)
 >Re: New C++ warning about comparing this==NULL (From: Doug Hill <email@hidden>)
 >Re: New C++ warning about comparing this==NULL (From: Roland King <email@hidden>)
 >Re: New C++ warning about comparing this==NULL (From: Doug Hill <email@hidden>)
 >Re: New C++ warning about comparing this==NULL (From: Jens Alfke <email@hidden>)
 >Re: New C++ warning about comparing this==NULL (From: Clark Cox <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