将这个代码释放内存分配给MULTIMAP? [英] Will this code free memory allocated for MULTIMAP?

查看:121
本文介绍了将这个代码释放内存分配给MULTIMAP?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个多图并且指向多图。说我有multimap
typedef multimap< class1,class2 *> LO_Index; 。我也有 LO_Index * _index; 这指向多图。要释放为这个maop分配的所有内存空间,我执行以下操作。我有一个重置功能,它执行以下操作释放所有的内存空间alocated为这 multimap

I have a multimap and a pointer to the multimap . Say i have multimap typedef multimap<class1, class2*> LO_Index; . I also have LO_Index * _index; Which points to multimap. To Free all the memory spaces allocated for this maop I am performing the following operation . I have a Reset function which does the following to free all the memory space alocated for this multimap:

    for ( LO_Index ::iterator i = _index->begin(); i != _index->end(); i++ )
            delete (*i).second;

    // Delete all entries in the index
    _index->erase( _index->begin(), _index->end() );

在set的情况下,我读取的是setname.clear()实际上不会释放所有分配的空间 。但在 http://www.cplusplus.com/reference/stl/multimap/erase/ 我发现擦除

What i have read in case of set is that setname.clear() actually doesnot free all the allocated space . But in http://www.cplusplus.com/reference/stl/multimap/erase/ I found about erase that


这有效地减少了容器大小除以元素数量
删除,调用每个元素的析构函数。

This effectively reduces the container size by the number of elements removed, calling each element's destructor.

所以我想它会释放分配的空间。

So i guess it will actually free the allocated spaces. But I want to confirm whether the code written in my reset function is actually freeing the memory or not.

推荐答案

对于多重映射, delete(* i).second无法编译,因为只能通过指针删除对象,而int不是指针。

For a multimap, delete (*i).second can't compile because you can only delete objects via a pointer and an int is not a pointer.

关于擦除功能:因为你担心,从容器中删除条目。分配器可能决定不返回内存,但是将其缓存以便重用,但这只在有限的情况下有效。

About the erase function: as far as you are concerned, the entries are removed from the container. The allocator might decide not to return the memory, but cache it for reuse, however, this matters only in limited cases.

这篇关于将这个代码释放内存分配给MULTIMAP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆