• Advertisement

Archived

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

std::vector

This topic is 5723 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
quote:
Original post by cedricl
Then, *CPolygon == NULL



What? CPolygon is a class...

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

  • Advertisement