在下一个示例中我如何释放内存。vt.pop_back() 删除 vt 中的元素,但不会释放内存。delete vt[i] 不起作用,它给了我分段错误。
#include <vector>
#include <unistd.h>
using namespace std;
const int SIZE = 1000000;
class Test
{
public:
Test();
~Test();
private:
int *arr;
};
int main()
{
vector<Test *> vt;
for(int i = 0; i < 100; i++)
vt.push_back(new Test());
sleep(10);
return 0;
}
Test::Test()
{
arr = new int[SIZE];
}
Test::~Test()
{
delete[] arr;
}