本文作者:StubbornHuang
版权声明:本文为站长原创文章,如果转载请注明原文链接!
原文标题:C++ – queue存储动态指针时正确释放内存
原文链接:https://www.stubbornhuang.com/818/
发布于:2020年05月06日 16:30:06
修改于:2020年10月10日 8:52:42

1 代码示例
#ifdef _DEBUG
#define DEBUG_CLIENTBLOCK new( _CLIENT_BLOCK, __FILE__, __LINE__)
#else
#define DEBUG_CLIENTBLOCK
#endif
#define _CRTDBG_MAP_ALLOC
#include <crtdbg.h>
#ifdef _DEBUG
#define new DEBUG_CLIENTBLOCK
#endif
//#include "vld.h" // visual leak detector 另一个内存泄漏检测工具
#include <iostream>
#include <queue>
#include <string>
using namespace std;
class Example
{
public:
Example();
Example(int newIndex);
virtual ~Example();
int m_Index;
};
Example::Example()
{
}
Example::Example(int newIndex)
{
m_Index = newIndex;
}
Example::~Example()
{
}
int main()
{
std::queue<Example*> MyTestQueue;
for (unsigned int i = 0; i < 10; ++i)
{
std::string tempStr = std::to_string(i);
Example* pExample = new Example(i);
MyTestQueue.push(pExample);
}
while (MyTestQueue.size() != 0)
{
Example* pExample = MyTestQueue.front();
MyTestQueue.pop();
delete pExample;
pExample = NULL;
}
_CrtDumpMemoryLeaks();
getchar();
return 0;
}
当前分类随机文章推荐
- C++ – 字节数组byte[]或者unsigned char[]与bool的相互转换 阅读771次,点赞1次
- C++ - linux编译C++代码出现error: use of deleted function std::atomic
::atomic(const std::atomic 阅读2005次,点赞0次&) - C++ - std::string与std::wstring相互转换 阅读1556次,点赞0次
- C++11/std::thread - 线程管理join/detach 阅读2100次,点赞0次
- C++ - 使用模板和智能指针构建一个双向链表工具类 阅读666次,点赞0次
- C++ - std::unordered_map中使用结构体或者vector等复杂的数据结构作为Key值 阅读168次,点赞0次
- C++STL容器 - std::vector元素访问方式总结 阅读629次,点赞0次
- C++ - std::map - 存储动态指针时正确释放内存 阅读3899次,点赞1次
- C++ - std::string替换字符串中所有指定的子字符串 阅读1422次,点赞1次
- GCC/GG++中编译优化选项-O -O0 -O1 -O2 -O3 -Os -Ofast -Og -Oz各自的区别和作用 阅读2375次,点赞2次
全站随机文章推荐
- 资源分享 - AI Game Engine Programming , First Edition 英文高清PDF下载 阅读741次,点赞0次
- 资源分享 - Beginning DirectX 11 Game Programming 英文高清PDF下载 阅读1085次,点赞0次
- Duilib - 超链接文本 阅读3281次,点赞0次
- 资源分享 - Implicit Curves and Surfaces - Mathematics, Data Structures and Algorithms 英文高清PDF下载 阅读1471次,点赞1次
- Duilib - pos和padding属性设置的顺序 阅读2282次,点赞0次
- C++ – UTF8编码下的全角字符转半角字符 阅读1418次,点赞0次
- C++ - 将std::vector中的数值拷贝到数组中 阅读1786次,点赞1次
- TensorRT - 转换onnx模型出现Slice_74 requires bool or uint8 I/O but node can not be handled by Myelin错误 阅读170次,点赞0次
- Pytorch - torch.cat函数 阅读97次,点赞0次
- UnrealEngine4 - 将FTexture2DRHIRef保存为图片 阅读3115次,点赞0次
评论
164