Sign in to follow this  
TheComet

Are you sure it's alive?

Recommended Posts

ApochPiQ    23058

I'm also wondering how often you will get a null this pointer in practice.
 
It seems quite likely that you would instead receive an arbitrary (but still incorrect) pointer value...


if ((unsigned)(this) < 0x1000) return;

Share this post


Link to post
Share on other sites
TheComet    3900

I'm also wondering how often you will get a null this pointer in practice.

 

It seems quite likely that you would instead receive an arbitrary (but still incorrect) pointer value...

 

You should never get a null or incorrect "this" pointer. The fact that you have access to "this" already means the object exists in memory and is valid.

Share this post


Link to post
Share on other sites
wintertime    4108
#include <iostream>

class Test {
public:
  void printthis();
};

void Test::printthis() {
  std::cout << this << std::endl;
}

void lalala(Test* p) {
  p->printthis();
}

int main() {
  lalala(0);
  return 0;
}

If that guy also got such wrong code in his project... biggrin.png

Share this post


Link to post
Share on other sites
Hodgman    51324

I think I wrote that exact same hack back in the 90's when I was first learning C++.

Somewhere I had a NULL pointer and was calling functions on it, which means you end up inside that function with a NULL this value.

I diagnosed this symptom (this is null), and "fixed" it by inserting the if statement from the OP... nevermind the actual cause of the bug... unsure.png

Share this post


Link to post
Share on other sites
swiftcoder    18437

You should never get a null or incorrect "this" pointer. The fact that you have access to "this" already means the object exists in memory and is valid.

 

Unfortunately, not true. Try the following program:

#include <iostream>
 
struct X
{
  void print() {
    std::cout << "pointer: " << (void*)this << std::endl;
  }
};
 
int main() {
  X *x = NULL;
  x->print();
 
  X *y;
  y->print();
}

On my machine, that produces the following:

$ clang++ null.c++ 
$ ./a.out 
pointer: 0
pointer: 0x7fff5d4e5b68

Which goes back to my argument that the common case is not in fact NULL, but an arbitrary pointer value, because in my mind it is much more likely to accidentally call a function on an undefined pointer than a pointer which I have explicitly initialised to NULL (unless of course your dev platform always nulls pointers by default).

Share this post


Link to post
Share on other sites
ApochPiQ    23058

You should never get a null or incorrect "this" pointer. The fact that you have access to "this" already means the object exists in memory and is valid.




Besides the null pointer example already given, consider the following:

struct Foo
{
    int Member;
    void DoStuff()
    {
        std::cout << this << std::endl;
    }
};

struct Bar
{
    int padding;
    int morepadding;
    Foo data;
};

int main()
{
    Bar* theobject = NULL;
    theobject->data.DoStuff();
}
My compiler prints 0x00000008 on a 32-bit build, for instance.

Share this post


Link to post
Share on other sites
Krohm    5031

I also did it a few times in the past, before I learnt to properly design object lifetimes.

But I still wonder about the delete this thing... that looked beyond words.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this