Serialization of binary string

I am trying to serialize binary string and immediately de-serialize it. The problem shows up when I am accessing the results of serialization. Why "testout " variable in the example below points to some garbage? I expect it to be a copy of "test", which it is not, although ipos variable correctly has value of 20.

Any suggestions, comments will be greatly appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include "stdafx.h"
#include <sstream>

int _tmain(int argc, _TCHAR* argv[])
{
	char* test = new char[20];
	memset(test,0x0,20);
	int i = 5;
	memcpy (test, &i, sizeof(int));
	std::ostringstream sout (std::ios_base::binary);
	sout.write(test, 20);
        int ipos = sout.tellp();

	// testout points to the start of some garbage....
	const char* testout = sout.str().data();

	delete [] test;
	return 0;
}

Problem solved, thanks.
Topic archived. No new replies allowed.