矢量::清除:内存问题

发布于 2024-10-27 13:25:51 字数 319 浏览 7 评论 0原文

Vector::Clear() 将删除向量数组中的元素。

问题是,如果我们在向量列表中传递对象,那么 Clear() 是否会删除对象的内存。

我发布了我的示例:

     CData *m_data = new CData();
     vector<CData*> m_logdata;
     m_logdata.push_back(m_data);
     m_logdata.clear();

这段代码会删除 m_data 分配的内存还是简单地删除向量列表中的元素?

问候, 卡蒂克

Vector::Clear() will erase the elements in the vector array.

The issue is that if we pass objects in vector list then Clear() will delete the memory of objects or not.

I post my sample:

     CData *m_data = new CData();
     vector<CData*> m_logdata;
     m_logdata.push_back(m_data);
     m_logdata.clear();

will this code delete the memory allocated by m_data or simply remove the element in the vector list?

Regards,
karthik

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

幽蝶幻影 2024-11-03 13:25:51

没有向量,但有向量。

vector.clear() 将破坏向量中的对象。如果这些对象是指针,则不会删除所指向的对象。

struct Foo {
};

vector<Foo> vec1;
vector<Foo *> vec2;

vec1.push_back(Foo());
vec2.push_back(new Foo());

vec1.clear(); // Will destruct the Foo instances
vec2.clear(); // Will not delete the pointed to Foo instances

在您编辑的帖子上:

m_logdata.clear();

这不会删除您的 CData 实例。它只会破坏指针,这是一个空操作。

编辑:回应评论。

for (size_t p = 0; p < m_logdata.size(); ++p)
  delete m_logdata[p];
m_logdata.clear();

There's no Vector, there is a vector though.

vector.clear() will destruct the objects in the vector. If these objects are pointers, it will not delete the pointed to objects.

struct Foo {
};

vector<Foo> vec1;
vector<Foo *> vec2;

vec1.push_back(Foo());
vec2.push_back(new Foo());

vec1.clear(); // Will destruct the Foo instances
vec2.clear(); // Will not delete the pointed to Foo instances

On your edited post:

m_logdata.clear();

This will not delete your CData instance. It'll simply destruct the pointer which is a no-op.

EDIT: In response to comment.

for (size_t p = 0; p < m_logdata.size(); ++p)
  delete m_logdata[p];
m_logdata.clear();
归属感 2024-11-03 13:25:51

这个问题很模糊......但无论哪种方式,我猜您想知道在调用 clear() 时存储在 vector 中的指针是否会自动删除。

事实并非如此。无论您在 vector 中存储什么内容,当 clear() 发生时,都会调用其析构函数。原始类型(例如指针)的析构函数不执行任何操作。

从我的 SGI 实现来看:

/**
 *  Erases all the elements.  Note that this function only erases the
 *  elements, and that if the elements themselves are pointers, the
 *  pointed-to memory is not touched in any way.  Managing the pointer is
 *  the user's responsibilty.
 */
void
clear() { erase(begin(), end()); }

要正确删除任何 STL 容器中的项目指针,最简单的方法是创建一个函子并将其应用于每个项目:

#include <iostream>
#include <cstdlib>
#include <vector>
#include <functional>
#include <algorithm>

template <typename ValueT>
struct func_delete : public std::unary_function<ValueT,void>
{
  void operator()( ValueT& item ) { delete item; }
};

struct Foo
{
  ~Foo() { std::cout << "Foo::~Foo()" << std::endl; }
};

template <typename ContainerT>
void delete_container( ContainerT& container )
{
  std::for_each( container.begin(), container.end(), func_delete<typename ContainerT::value_type>() );
}

int main( int argc, char** argv )
{
    std::vector<Foo*> foos;

    foos.push_back( new Foo );
    foos.push_back( new Foo );
    foos.push_back( new Foo );

    // Either of these two lines will work
    //std::for_each( foos.begin(), foos.end(), func_delete<Foo*>() );
    //delete_container( foos );

    foos.clear();

    return EXIT_SUCCESS;
}

The question is quite vague... but either way, I guess that you are wondering if pointers stored in a vector would automatically be deleted when clear() is called.

This is not the case. Whatever you store in a vector will have its destructor called when clear() occurs. And the destructor of a primitive type (such as a pointer) does.. nothing.

From my SGI implementation:

/**
 *  Erases all the elements.  Note that this function only erases the
 *  elements, and that if the elements themselves are pointers, the
 *  pointed-to memory is not touched in any way.  Managing the pointer is
 *  the user's responsibilty.
 */
void
clear() { erase(begin(), end()); }

To properly delete items pointers in any STL container, the easiest way is to create a functor and apply it on every item:

#include <iostream>
#include <cstdlib>
#include <vector>
#include <functional>
#include <algorithm>

template <typename ValueT>
struct func_delete : public std::unary_function<ValueT,void>
{
  void operator()( ValueT& item ) { delete item; }
};

struct Foo
{
  ~Foo() { std::cout << "Foo::~Foo()" << std::endl; }
};

template <typename ContainerT>
void delete_container( ContainerT& container )
{
  std::for_each( container.begin(), container.end(), func_delete<typename ContainerT::value_type>() );
}

int main( int argc, char** argv )
{
    std::vector<Foo*> foos;

    foos.push_back( new Foo );
    foos.push_back( new Foo );
    foos.push_back( new Foo );

    // Either of these two lines will work
    //std::for_each( foos.begin(), foos.end(), func_delete<Foo*>() );
    //delete_container( foos );

    foos.clear();

    return EXIT_SUCCESS;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文