• 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 15:32:15 -0800


On Dec 2, 2015, at 12:00 PM, Jonathan Prescott <email@hidden> wrote:

In C++, “this” is a read-only (const) pointer setup during construction of the instance.  Once an instance of a class is successfully created, the “this” pointer is guaranteed to be non-null for the lifetime of the instance.  

No. It’s not a _part of_ the instance, it’s a _pointer to_ the instance. It only exists as an invisible parameter passed to a method.

For example, the method foo::method(int x) is internally implemented as a function [with a mangled name] that takes a parameter list (foo *this, int x). And calling f->method(1) is exactly like calling that function with parameter list (f, 1). Assuming the method is non-virtual. If it’s virtual, this gets more complicated.

I tried out a couple of ways of trying to modify the “this" pointer of an existing class instance, and the clang and gcc compilers would not allow that operation to compile.

Again, ‘this’ doesn’t belong to the instance. It’s simply a function parameter.

Here’s a simple way to call a method with a NULL ‘this’:

struct foo {
void method() {
printf(“this = %p\n”, this);
}
}

foo *f = NULL;
f->method();

—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: Zack Morris <email@hidden>
    • Re: New C++ warning about comparing this==NULL
      • From: Doug Hill <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>)

  • 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