#ifndef UNICODE
#define UNICODE
#endif
#include <iostream>
#include <Windows.h>
#include <queue>
using namespace std;
void addSomeContent(queue<TCHAR*> &s)
{
static int counter=0;
TCHAR* buffer = new TCHAR[250]; //Allocate memory on heap
wsprintf(buffer,TEXT("foo%d"),counter);
s.push(buffer);
counter++;
if(counter < 10)
addSomeContent(s);
}
int main (void)
{
queue<TCHAR*> strings;
addSomeContent(strings);
while(!strings.empty())
{
wcout<<strings.front()<<endl;
strings.pop();
}
//Here I want to destroy the "buffer" from the function "addSomeContent"
wcout<<TEXT("Memory has been cleaned!\n");
system("pause");
return (0);
}
如果我在函数末尾删除了宽字符数组,我就无法处理引用它的队列。现在,我的简单程序可以编译并且工作正常,但显然在堆中保留垃圾并不被认为是一种安全的编程实践。
上次使用后如何删除“缓冲区”?