带有向量的新运算符

发布于 2024-10-14 14:02:10 字数 903 浏览 6 评论 0原文

这些问题相对简单。使用向量时,推回新元素时是否应该使用 new 运算符?我应该调用哪个释放方法?我的意思是这样的:

// Release method: 1.
void ReleaseMethodOne( vector< int * > &ThisVector )
{
    // Clear out the vector.
    ThisVector.clear( );
    return;
}

// Release method: 2.
void ReleaseMethodTwo( vector< int * > &ThisVector )
{
    // Clear out the vector.
    for( unsigned uIndex( 0 ); uIndex < ThisVector.size( ); uIndex++ )
    {
        delete ThisVector.at( uIndex );
    }
    return;
}

int main( )
{
    vector< int * > Vector;

    // Add a new element.
    Vector.push_back( new int( 2 ) );

    // More code...

    // Free the elements before exiting. Which method should I call here?
    ReleaseMethodOne( Vector ); // This one?
    ReleaseMethodTwo( Vector ); // Or this one?

    return 0;
}

我不久前开始学习向量,我学习的书上说向量的 clear( ) 方法调用每个元素的析构函数。这适用于 new 运算符吗?

These questions are relatively straight forward. When using vectors, should I use the new operator when pushing back a new element? And which release method should I call? Here's what I mean:

// Release method: 1.
void ReleaseMethodOne( vector< int * > &ThisVector )
{
    // Clear out the vector.
    ThisVector.clear( );
    return;
}

// Release method: 2.
void ReleaseMethodTwo( vector< int * > &ThisVector )
{
    // Clear out the vector.
    for( unsigned uIndex( 0 ); uIndex < ThisVector.size( ); uIndex++ )
    {
        delete ThisVector.at( uIndex );
    }
    return;
}

int main( )
{
    vector< int * > Vector;

    // Add a new element.
    Vector.push_back( new int( 2 ) );

    // More code...

    // Free the elements before exiting. Which method should I call here?
    ReleaseMethodOne( Vector ); // This one?
    ReleaseMethodTwo( Vector ); // Or this one?

    return 0;
}

I've started learning vectors not long ago and the book I was learning from said that the vector's clear( ) method called each of the elements destructor. Do this apply to the new operator?

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

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

发布评论

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

评论(6

萌化 2024-10-21 14:02:10

STL 容器存储您提供给它们的对象的副本,在示例中是指针。它们永远不会释放您显式分配的任何内存。您必须自己释放该内存,因此应使用第二种“释放”方法。

当然,您不需要为每个 intnew。只需使用 vector 即可 - 您根本不必处理手动内存管理。

STL containers store copies of the objects you give to them, pointers in your example. They never release any memory you explicitly allocated. You have to deallocate that memory yourself, so the second "release" method should be used.

Of course, you don't need to new every int. Just use vector<int> instead - you won't have to deal with manual memory management at all.

倥絔 2024-10-21 14:02:10

clear 方法确实会调用析构函数。但是,您的向量正在存储指针,并且指针的析构函数是一个微不足道的无操作。它调用删除

因此,仅调用 clear 不会释放使用 new 分配的所有 int 对象的内存。您需要删除它们。

如果您使用智能指针而不是普通指针,那么所指向的对象将在适当的时间被释放,而无需执行任何特殊操作。

The clear method will indeed call destructors. However, your vector is storing pointers, and the destructor for pointers is a trivial no-op. It does not call delete.

Therefore, simply calling clear will not free the memory for all the int objects you allocated with new. You need to delete them.

If you use a smart pointer instead of an ordinary pointer, then the pointed-at objects will get freed at the appropriate time without you having to do anything special.

有木有妳兜一样 2024-10-21 14:02:10

对于您在那里所做的事情,您需要使用 ReleaseMethodTwo。调用元素的析构函数意味着包含的类(不包含的指针)将失去作用域并调用其析构函数。

据我所知,STL 容器不会为您调用delete。如果您分配并传入指针,则需要取消分配。

For what you are doing there you would need to use ReleaseMethodTwo. What it means by saying that it calls the element's destructor is that contained classes (not contained pointers) will lose scope and have their destructors called.

No STL container will ever call delete for you, so far as I know. If you allocate and pass in a pointer, you will need to deallocate.

氛圍 2024-10-21 14:02:10

您应该使用ReleaseMethodTwo(),因为如果您已经分配了内存,那么您有责任删除它。

std::vector::clear() 仅删除向量中的元素。它不会对被删除的元素调用delete

You should use ReleaseMethodTwo(), because if you've allocated the memory, then it's your responsibility to delete it.

std::vector::clear() only erases the elements from the vector. It doesn't call delete on the elements being erased!

烈酒灼喉 2024-10-21 14:02:10

正如其他人所回答的,向量确实调用每个元素的析构函数。在您的情况下,元素是指针,因此它不会释放内存。
对于您的应用程序,最好的选择是使用引用计数智能指针,例如 boost:shared_ptr。一旦不再有指向元素的引用(如您编写的示例),内存将自动释放。

PS:不要使用“非引用计数”智能指针,例如带有向量的 std::auto_ptr 。

As answered by others, vector does call the destructor of each element. In your case, the elements are pointers and hence it will not free the memory.
For your application, the best choice is to use a reference counting smart pointer like boost:shared_ptr. Once there are no more references pointing to the elements (like the example you had written), the memory will be freed automagically.

PS: Do not use a "non-reference counting" smart pointer like std::auto_ptr with vector.

何以笙箫默 2024-10-21 14:02:10

如果您使用的是 vactor ,那么您将拥有一个指针向量,并且您必须为每个元素分配内存并自己释放该内存。除非要在向量中存储的类型的大小非常大,否则使用指针向量没有多大意义。

实际上,当您执行 vector::push_back(val) 时,它是否会使用复制构造函数 T::T(T &orig) 并在执行 clear() 时调用所有元素的析构函数

if you are using vactor<int *> than you'll have a vector of pointers and you have to allocate memory for each elements and free this memory yourself also. It doesn't make much sense to use vector of pointers unless the size of type you want to store in the vector is quite big.

Actually when you do vector<T>::push_back(val) is it going to store a copy of val using a copy constructor T::T(T &orig) and call destructor for all elements when you do clear()

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