#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[30];
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();
}
system("pause");
return (0);
}
Output:
foo0
♦
Desired:
foo0
foo1
.
.
.
foo9
Where am I wrong?
The reason is that your
bufferis a local variale on stack. Once you leave the function, it will expire.If you really want to do this way, create it on heap
TCHAR *buffer = new TCHAR[30];. And you might need todelete[]it after some point.However, I think using some built-in types or stl containers instead of manipulating pointers will make your code more readable and manageable.