本文作者: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++ - 使用正则判断字符串是否全是中文 阅读1192次,点赞0次
- C++STL容器 – std::vector容器修改、元素操作总结 push_back,emplace_back,emplace,insert,erase,pop_back,clear,resize,swap 阅读1044次,点赞1次
- C++ - sleep睡眠函数总结 阅读1123次,点赞0次
- C++ - std::string字符串格式化方法总结 阅读949次,点赞0次
- C++/OpenCV - 详解如何一步步将OpenCV的cv::Mat转换成深度学习模型推理所需的输入数据 阅读201次,点赞0次
- C++ - 判断本机文件是否存在的方式总结 阅读5035次,点赞0次
- C++11/std::condition_variable - 生产者消费者模型 阅读2883次,点赞0次
- C++ - Windows/Linux生成uuid(通用唯一识别码) 阅读1940次,点赞2次
- C++ - GBK编码下的全角字符转半角字符 阅读1660次,点赞0次
- C++ - 使用标准库实现事件和委托,信号和槽机制 阅读410次,点赞0次
全站随机文章推荐
- Centos - 更换SSH端口 阅读2912次,点赞1次
- Python - opencv-python保存视频时出现Failed to load OpenH264 library: openh264-1.8.0-win64.dll错误 阅读1049次,点赞0次
- 资源分享 - Virtual Clothing - Theory and Practice 英文PDF下载 阅读1151次,点赞0次
- 资源分享 - Div, Grad, Curl, and All That - An Informal Text on Vector Calculus , Third Edition 英文高清PDF下载 阅读1455次,点赞0次
- Google Adsense - 从Google Adsense开通到第一个10美元我用了一年时间 阅读2092次,点赞2次
- 资源分享 - 计算机图形学 第2版,Fundamentals of Computer Graphics(Second Edition) 中文版PDF下载 阅读3876次,点赞0次
- MindSpore - LeNet5的MindSpore实现 阅读1100次,点赞0次
- C++ – 字节数组byte[]或者unsigned char[]与bool的相互转换 阅读1055次,点赞1次
- OnnxRuntime - 模型部署笔记1,OnnxRuntime简介 阅读189次,点赞0次
- Duilib - 为列表控件UIList添加列表子项右键点击消息响应功能 阅读473次,点赞0次
评论
169