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);
}
std::vector
I was playing with vectors last night... and why isn´t z == NULL?
(yes, a msgbox popsup... )
[edit: added source tags]
[edited by - Magmai Kai Holmlor on August 19, 2002 1:07:22 AM]
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]
[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]
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
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
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
CPolygon **z = &a[0]
Then, *CPolygon == NULL
Cédric
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
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
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
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..
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
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
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement