Archived

This topic is now archived and is closed to further replies.

Zoot

std::vector

Recommended Posts

I was playing with vectors last night... and why isn´t z == NULL?
  
std::vector<CPolygon*> a;
a.push_back(new CPolygon);
a[0]->v1 = vector(0,0,1);

CPolygon *z = a[0];

delete a[0];
a[0] = NULL;
a.erase(&a[0]);

if (z != NULL)
{
MessageBox(0,"damn" , "...",0);
}
  
(yes, a msgbox popsup... ) [edit: added source tags] [edited by - Magmai Kai Holmlor on August 19, 2002 1:07:22 AM]

Share this post


Link to post
Share on other sites
CPolygon*& z = a[0];

[edit] that'll work if you dont erase() it. else funny things will happen.

[edited by - sark on August 18, 2002 2:53:42 PM]

Share this post


Link to post
Share on other sites
you delete the object, z points to, but you don''t affect z itself by that, it still has the same value in, an address. that address got invalid, as you deleted the data there, but it doesn''t affect z anyways..

how about

a.push_back(CPolygon);
a[0].v1 = vector(0,0,1); ? that way the vector stores the actual polygons, and takes care of killing them .. (with erase, or clear, or when the vector dies..)

"take a look around" - limp bizkit
www.google.com

Share this post


Link to post
Share on other sites
davepermen is right. z is still pointing to an object that has been deleted. If you want zto be NULL when you call a[0] = NULL, you need a pointer to a pointer.

CPolygon **z = &a[0]

Then, *CPolygon == NULL

Cédric

Share this post


Link to post
Share on other sites
quote:
Original post by davepermen
you delete the object, z points to, but you don''t affect z itself by that, it still has the same value in, an address. that address got invalid, as you deleted the data there, but it doesn''t affect z anyways..

how about

a.push_back(CPolygon);
a[0].v1 = vector(0,0,1); ? that way the vector stores the actual polygons, and takes care of killing them .. (with erase, or clear, or when the vector dies..)

"take a look around" - limp bizkit
www.google.com


vector != std::vector

Share this post


Link to post
Share on other sites
Oops. I meant,

CPolygon **z = &a[0];
a[0] = NULL;

Then, *z == NULL,

And about vector != std::vector, may I suggest changing the name of vector? You''re asking for trouble as it is...

Cédric

Share this post


Link to post
Share on other sites
quote:
Original post by cedricl
Oops. I meant,

CPolygon **z = &a[0];
a[0] = NULL;

Then, *z == NULL,

And about vector != std::vector, may I suggest changing the name of vector? You''re asking for trouble as it is...

Cédric


...and if you don´t know where z is? suppose it is a pointer to a door in game.. and the door gets blow up..

Share this post


Link to post
Share on other sites
What do you mean?

If *z (not z) is a pointer to a door that gets blown-up and set to NULL, then *z == NULL, as long as z was in fact assigned to the pointer to the door.

Cédric

Share this post


Link to post
Share on other sites
quote:
Original post by cedricl
If *z (not z) is a pointer to a door that gets blown-up and set to NULL, then *z == NULL

Only pointers can be NULL, so "z" might equal NULL, but never "*z" (the object which z points at.

The reason for the OP''s problem is that he has set the pointer returned by a[0] to NULL, but z *never* gets set to NULL. These are two different and distinct pointers.

Share this post


Link to post
Share on other sites
*Sigh* I understand where your confusion comes from, but let me re-repost my example:

CPolygon **z = &a[0];
a[0] = NULL;

Then, *z == NULL,

So... *z can be == NULL. I admit that it wasn''t clear in my last post.

Cédric

Share this post


Link to post
Share on other sites
quote:
Original post by cedricl
*Sigh* I understand where your confusion comes from, but let me re-repost my example:

CPolygon **z = &a[0];
a[0] = NULL;

Then, *z == NULL,

So... *z can be == NULL. I admit that it wasn''t clear in my last post.

Cédric


when i do:
CPolygon **z = &a[0];
a[0] = NULL;

Then: z =! NULL

Share this post


Link to post
Share on other sites
quote:
Original post by Magmai Kai Holmlor
Original post by Zoot
I was playing with vectors last night... and why isn´t z == NULL?

because the computer isn't clairsentient?
Somewhere you have to set z to null, if you want it to be null.




I want z to be the same as a[0]....

[edited by - Zoot on August 19, 2002 9:46:50 AM]

Share this post


Link to post
Share on other sites
quote:
Original post by Zoot
I want z to be the same as a[0]....



Sounds like you want a reference to pointer.


    
std::vector<CPolygon *> a;
a.push_back(new CPolygon);
a[0]->v1 = vector(0,0,1);
CPolygon *&z = a[0];
delete a[0];
a[0] = NULL;
// Now z is NULL



Whatever you do to a[0], the same thing will happen to z because
z is a reference (alias to a[0]).



Kami no Itte ga ore ni zettai naru!

Share this post


Link to post
Share on other sites
quote:
Original post by tangentz
Original post by Zoot
I want z to be the same as a[0]....


Sounds like you want a reference to pointer.



      
std::vector<CPolygon *> a;
a.push_back(new CPolygon);
a[0]->v1 = vector(0,0,1);
CPolygon *&z = a[0];
delete a[0];
a[0] = NULL;
// Now z is NULL



Whatever you do to a[0], the same thing will happen to z because
z is a reference (alias to a[0]).



Kami no Itte ga ore ni zettai naru!



No, not working.

Share this post


Link to post
Share on other sites
quote:
Original post by Zoot
No, not working.



Even though I typed the code "cold", I know it works.

It wouldn''t kill you to explain what is "not working",
would it?



Kami no Itte ga ore ni zettai naru!

Share this post


Link to post
Share on other sites
quote:
Original post by Zoot
when i do:
CPolygon **z = &a[0];
a[0] = NULL;

Then: z =! NULL

No, but *z IS equal to NULL (NOT z --- as MKM pointed out, z will NEVER be equal to NULL unless you write it explicitly, or through another pointer). You are dereferencing ONCE a pointer to a pointer to a CPolygon. So, what you get is a pointer to a CPolygon, which CAN be NULL.

And tangentz''s method is the same as mine, except with a reference. It should also work.

Cédric

Share this post


Link to post
Share on other sites