从地图中删除指针 [英] Delete pointers from a map
问题描述
有一张地图,它将 int
映射到测试*
。
所有 Test *
指针在之前分配并稍后分配给映射。然后,我删除
的值,并将它们设置为 null
。 / p>
之后,它检查一个
的有效性,它应该是 null
。 但,
一个不是
null
。
#include< QString>
#include< QMap>
#include< QDebug>
class Test {
QString name;
public:
Test(const QString& name):name(name){}
QString getName()const {return name; }
};
int main(){
QMap< int,Test *>地图;
Test * one = new Test(one);
Test * two = new Test(two);
Test * three = new Test(three);
map.insert(1,one);
map.insert(2,two);
map.insert(3,three);
for(auto itr = map.begin(); itr!= map.end(); itr ++){
Test * x = * itr;
if(x){
delete x;
x = 0; // **设置null为指针** //
}
}
if(one)// **这里一个不是0? ** //
qDebug()<< one-> getName()<< endl; // **然后这里崩溃** //
}
如何可以使用
它是否固定?第二个问题是,是否正确 delete
?
在循环中,变量 x
只在循环内。当你将 NULL
设置为 NULL
。
你应该设置通过解引用迭代器返回的引用为 NULL
:
* itr = nullptr;
这将使地图中的指针 NULL $ c $
当你有两个指针时,它类似这样:
+ ----- +
|一| - \
+ ----- + | + --------------- +
> - > |测试实例|
+ ----- + | + --------------- +
| x | --- /
+ ----- +
如果您设置了指针它看起来像这样:
+ ----- +
|一| --- \
+ ----- + | + --------------- +
> - > |测试实例|
+ ----- + + --------------- +
| x |
+ ----- +
变量 x
NULL
但是变量一个
仍指向对象。如果对象已删除,则取消引用该指针,将导致未定义的行为
There is a map, it maps an int
to a Test*
.
All Test*
pointers are allocated before and assigned to the map later. Then, I'm delete
ing values of map and set them to null
.
After that, It checks validity of one
, and it supposed to be null
. But, one
is not null
.
#include <QString>
#include <QMap>
#include <QDebug>
class Test {
QString name;
public:
Test(const QString &name) : name(name) {}
QString getName() const { return name; }
};
int main() {
QMap<int, Test*> map;
Test *one = new Test("one");
Test *two = new Test("two");
Test *three = new Test("three");
map.insert(1, one);
map.insert(2, two);
map.insert(3, three);
for (auto itr = map.begin(); itr != map.end(); itr++) {
Test *x = *itr;
if (x) {
delete x;
x = 0; // ** Sets null to the pointer ** //
}
}
if (one) // ** Here one is not 0 ?! ** //
qDebug() << one->getName() << endl; // ** And then here crashes ** //
}
I think, I missed something when I'm delete
ing them in the loop.
How can it fixed?
Second question is, is it correctly delete
s the allocated pointers?
In the loop, the variable x
is a local pointer only inside the loop. When you set that to NULL
you don't actually set any other pointers to NULL
.
What you should to is to set the reference returned by dereferencing the iterator to NULL
:
*itr = nullptr;
This will make the pointer in the map be NULL
, but the other pointers will still be pointing to a now deallocated memory area.
When you have two pointers, it kind of looks like this:
+-----+ | one | ---\ +-----+ | +---------------+ >--> | Test instance | +-----+ | +---------------+ | x | ---/ +-----+
If you set one of the pointers it looks like this:
+-----+ | one | ---\ +-----+ | +---------------+ >--> | Test instance | +-----+ +---------------+ | x | +-----+
The variable x
is NULL
but the variable one
still points to the object. And if the object has been deleted then dereferencing that pointer you will cause undefined behavior.
这篇关于从地图中删除指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!