SunStudio 11 C++ std::stringstream stream pointer problem
Example program to demonstrate that the stream pointer for
std::stringstream streams is not always set correctly after writing
a char array to the stream.
Problem is present in SunStudio 11:
Sun C++ 5.8 Patch 121017-04 2006/08/02
No special compile options, standard C++ library
[code]
#include <string>
#include <sstream>
#include <iostream>
int main( int argc, char *argv[] )
{
std::stringstream os;
char cbuf[1000];
for ( int i=0;i<sizeof(cbuf);i++) cbuf[i]='a';
// Write contents of char array to the stringstream. The stream
// pointer is not incremented. The flush() call doesn't help.
os.write(cbuf, sizeof(cbuf));
os.flush();
std::cout ><< "Position after char* write is wrong: " \
<< os.tellp() << std::endl;
// After appending to the stringstream the stream pointer is
// now correct.
os << "A";
std::cout << "Position after std::string append is ok: " \
<< os.tellp() << std::endl;
return 0;
}
[/code]
# 5
Hmm. I get 1000 and 1001 with 5.8:
$ CC -V t.cpp && ./a.out
CC: Sun C++ 5.8 Patch 121017-01 2005/12/11
/amd/packages/mdx/solaris/SUNWspro/C++5.8j1/prod/bin/c++filt: Sun C++ 5.8 2005/10/13
ccfe: Sun C++ 5.8 Patch 121017-01 2005/12/11
ld: Software Generation Utilities - Solaris Link Editors: 5.10-1.480
Position after char* write is wrong: 1000
Position after std::string append is ok: 1001
# 6
So perhaps it is a regression in one of the CC patches 121017-02
to -04?
Or a difference in the libCstd.so? On my Solaris 10 6/06 system:
$ ls -l /usr/lib/libC*
-rwxr-xr-x1 rootbin100764 Jan 23 2005 /usr/lib/libC.so.3*
-rwxr-xr-x1 rootbin391268 Mar 10 2006 /usr/lib/libC.so.5*
-rwxr-xr-x1 rootbin63628 Mar 10 2006 /usr/lib/libCrun.so.1*
-rwxr-xr-x1 rootbin1899836 Mar 10 2006 /usr/lib/libCstd.so.1*
# 7
I just compiled this code on Solaris 10, Solaris 10 6/06 s10s_u2wos_09a SPARC
./test_sstream
Position after char* write is wrong: -1
Position after std::string append is ok: 1001
CC -V
CC: Sun C++ 5.8 Patch 121017-04 2006/08/02
Same result on pretty well patched Solaris 9 and 8. With the same compiler.