• 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 21:28:25 -0800


On Dec 2, 2015, at 5:18 PM, Doug Hill <email@hidden> wrote:

"Certain other operations are described in this International Standard as undefined (for example, the effect of dereferencing the null pointer). [Note: this International Standard imposes no requirements on the behavior of programs that contain undefined behavior. ]”

Not relevant; I already explained in a previous reply that this does not dereference a null pointer. You can call nonvirtual methods on NULL pointers just fine without any runtime issues … just as long as you don’t access any member variables, of course.

The problem seems to be that you can make up C++ code that constructs a NULL pointer and then crash when you dereference it. My understanding is that the C++ spec unequivocally defines this as an error and therefor it shouldn’t be happening at all. 

It’s not a weird situation at all. Something returns you an object pointer. You call a method on that object. But the pointer you got happens to be NULL. Ergo, you’ve just called a method on a NULL pointer.
Widget *w = factory.getCurrentWidget();
Paint *p = w->getPaint();
If the currentWidget is NULL, the second line does this. In some circumstances it’s useful to be able to handle this — heck, we should be able to agree on this, since we use this handy behavior of messaging nil in Obj-C all the time. So Widget::getPaint() could check for this==nil and if so return NULL instead of crashing.

It’s not a big deal; I’ve adjusted my code not to do this. I was just curious where this tightening of the standard came from.

—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: Clark Cox <email@hidden>
    • Re: New C++ warning about comparing this==NULL
      • From: Marco S Hyman <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>)

  • 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