Skip to main content
gamedev.net
🔒 Locked

evil pointers...

Started by CodeZ Dec 18, 2002 at 3:31 AM 4 replies 300+ views
Original Post
CodeZ
CodeZ
What happen if I have a pointer (b) to a pointer (a) and I delete pointer a and set it to NULL. Is is then possible to check if pointer b exist by asking if it is NULL? Like this:
  

class Foo
{
  Model* a; // allocate the memory

}

class Window
{
  Model* b; // only a pointer

}

void main()
{
  Foo kFoo;
  kFoo.a = new Model;

  Window kWnd;
  kWnd.b = kFoo.a;

  delete kFoo.a;
  kFoo.a = NULL;

  if(kWnd.b != NULL)
  {
    // do something...

  }
}

  
MadKeithV
MadKeithV
No, it''s not. b will still be pointing to the same memory location, which is now invalid.
It's only funny 'till someone gets hurt.And then it's just hilarious.Unless it's you.
CodeZ
CodeZ
Ok, I though so. I don´t know how many times I have made that misstake...

Thanks.
davepermen
davepermen
quote:
Original post by CodeZ


    

class Foo
{
boost::shared_ptr<Model> a; // allocate the memory

}

class Window
{
boost::weak_ptr<Model> b; // only a pointer

}

void main()
{
Foo kFoo;
kFoo.a = boost::shared_ptr<Model>(new Model);

Window kWnd;
kWnd.b = kFoo.a;

kFoo.a.reset();

if(kWnd.b != NULL)
{
// do something...

std::cout<<"can not happen"<<std::endl;
}
}


Window::b gets set to 0 if a gets deleted (wich it gets due .reset())


if you want the data gets deleted when both Foo and Window die (=>if only Foo dies, Window gets the Model pointer for its own), use boost::shared_ptr in both classes..

"take a look around" - limp bizkit
www.google.com
If that's not the help you're after then you're going to have to explain the problem better than what you have. - joanusdmentia
My Page davepermen.net | My Music on Bandcamp and on Soundcloud
CodeZ
CodeZ
Humm... What is boost? A library or some sort? Is it good, does many people use it? Maybe something suited for me...

Thanks.
MadKeithV
MadKeithV
Boost is quite a well known library, HEAVILY technical, with some really great stuff. I don''t use it, but many people around here do.

Check www.boost.org for more information.
It's only funny 'till someone gets hurt.And then it's just hilarious.Unless it's you.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.