zhuwt2008 2015-03-04 14:14 采纳率: 33.3%
浏览 2215
已采纳

这个vector对象的资源该如何释放呢?

下面这个程序main函数有内存泄漏,运行内存检测工具valgrind可以看到(程序可以正常运行的),该如何释放vector的内存资源呢?
#include
#include
using namespace std;

class yitem
{
public:
yitem(const char *s1):m_data(s1) { m_str = new string("abc"); }
~yitem(void) { delete m_str; }
string & get_data() { return m_data; }

private:
string m_data;
string *m_str = nullptr;
};

int main()
{
vector v1;
yitem a("A");
v1.push_back(a);

printf("ok\n");
return 0;
}

  • 写回答

4条回答 默认 最新

  • oyljerry 2015-03-04 15:26
    关注

    yitem的拷贝构造函数要实现,处理好指针。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?