使用 C11 样式 for 循环删除向量元素时出错
Getting an error on delete[] of vector element using C11 style for loop
#include <iostream>
#include <vector>
using namespace std;
// Shows Factor Design Pattern
class Stooge{
public:
static Stooge* makeStooge(int choice); // static class function
virtual void slapStick() = 0; // implemented by derived
virtual ~Stooge(){}
};
class Larry : public Stooge{
public:
void slapStick(){
cout << "Larry: poke eyes" << endl;
}
~Larry(){}
};
class Moe : public Stooge{
public:
void slapStick(){
cout << "Moe: slap head" << endl;
}
~Moe(){}
};
class Curly : public Stooge{
public:
void slapStick(){
cout << "Curly: suffer abuse" << endl;
}
~Curly(){}
};
Stooge* Stooge::makeStooge(int choice){
switch(choice){
case 1: return new Larry;
break;
case 2: return new Moe;
break;
case 3: return new Curly;
break;
default:{
cout << "Enter valid choice next time!!";
return NULL;
}
}
}
int main(){
vector<Stooge*> roles;
int choice;
while(true){
cout << "Larry(1) Moe(2) Curly(3) Exit(0)" << endl;
cin >> choice;
if(choice==0)
break;
Stooge *s = Stooge::makeStooge(choice);
if(s)
roles.push_back(s);
}
for(auto r : roles)
r->slapStick();
// this will fail
for(auto x : roles)
delete[] x;
// this works
#if 0
for(int i=0; i<roles.size(); i++)
delete roles[i];
#endif
return 0;
}
当我 运行 上面的代码片段时,我遇到崩溃并显示以下错误消息:
a.out(48873,0x7fff75912000) malloc: *** error for object
0x7fab72500058: pointer being freed was not allocated
在 delete[]
的调用堆栈中,我看到 'x' 的有效(malloc'ed)地址,但无法理解为什么实际免费中存在问题
对比常规的 for 循环(没有 C11 功能可以正常工作)。这意味着我不完全理解如何使用 auto-for 循环功能 - 有人可以解释一下吗?
x
是 roles
中的单个元素。因为它是用 new
分配的,所以应该用 delete
释放它(就像第二个块一样),而不是 delete[]
.
#include <iostream>
#include <vector>
using namespace std;
// Shows Factor Design Pattern
class Stooge{
public:
static Stooge* makeStooge(int choice); // static class function
virtual void slapStick() = 0; // implemented by derived
virtual ~Stooge(){}
};
class Larry : public Stooge{
public:
void slapStick(){
cout << "Larry: poke eyes" << endl;
}
~Larry(){}
};
class Moe : public Stooge{
public:
void slapStick(){
cout << "Moe: slap head" << endl;
}
~Moe(){}
};
class Curly : public Stooge{
public:
void slapStick(){
cout << "Curly: suffer abuse" << endl;
}
~Curly(){}
};
Stooge* Stooge::makeStooge(int choice){
switch(choice){
case 1: return new Larry;
break;
case 2: return new Moe;
break;
case 3: return new Curly;
break;
default:{
cout << "Enter valid choice next time!!";
return NULL;
}
}
}
int main(){
vector<Stooge*> roles;
int choice;
while(true){
cout << "Larry(1) Moe(2) Curly(3) Exit(0)" << endl;
cin >> choice;
if(choice==0)
break;
Stooge *s = Stooge::makeStooge(choice);
if(s)
roles.push_back(s);
}
for(auto r : roles)
r->slapStick();
// this will fail
for(auto x : roles)
delete[] x;
// this works
#if 0
for(int i=0; i<roles.size(); i++)
delete roles[i];
#endif
return 0;
}
当我 运行 上面的代码片段时,我遇到崩溃并显示以下错误消息:
a.out(48873,0x7fff75912000) malloc: *** error for object 0x7fab72500058: pointer being freed was not allocated
在 delete[]
的调用堆栈中,我看到 'x' 的有效(malloc'ed)地址,但无法理解为什么实际免费中存在问题
对比常规的 for 循环(没有 C11 功能可以正常工作)。这意味着我不完全理解如何使用 auto-for 循环功能 - 有人可以解释一下吗?
x
是 roles
中的单个元素。因为它是用 new
分配的,所以应该用 delete
释放它(就像第二个块一样),而不是 delete[]
.