• 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: Clark Cox <email@hidden>
  • Date: Thu, 03 Dec 2015 17:17:17 -0800

> On Dec 3, 2015, at 01:10, Anders Montonen <email@hidden> wrote:
>
>
>> On 03 Dec 2015, at 1:43, Jens Alfke <email@hidden> wrote:
>>
>> 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.
>
> Is that standard-mandated behaviour or an optimization?

It is a legal optimization due to the fact that it is illegal to call a member function through a NULL pointer. i.e. no valid C++ code can ever result in “this" being NULL, so the compiler is free to assume that "this == NULL" is always false.

> If it is legal for any member function call to go via the this pointer, then the check is illegal. As a practical consideration, in this example (modified from the viva64 post to add a data member to the second base class), Xcode 7.1.1 produces a non-null this pointer from a null pointer call:
>
> #include <stdio.h>
>
> class FirstBase {
>    int firstBaseData;
> };
>
> class SecondBase {
>    int secondBaseData;
> public:
>    void Method()
>    {
>        if( this == 0 ) {
>            printf( "this == 0\n");
>        } else {
>            printf( "this != 0 (value: %p)\n", this );
>        }
>    }
> };
>
> class Composed1 : public FirstBase, public SecondBase {
> };
>
> int main()
> {
>    Composed1* object = 0;
>    object->Method();
> }
>
> -a
> _______________________________________________
> 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


 _______________________________________________
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>)
 >Re: New C++ warning about comparing this==NULL (From: Anders Montonen <email@hidden>)

  • Prev by Date: Do Source Trees work in 7.1.1?
  • Next by Date: Can't add *.proto files to a target
  • 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