#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);
}
関数の最後でワイド文字配列を削除した場合、それを参照するキューを処理できませんでした。今、私の単純なプログラムはコンパイルされて正常に動作しますが、明らかにゴミをヒープに保持することは安全なプログラミング手法とは見なされません。
前回使用した直後の「バッファ」を削除するにはどうすればよいですか?