stringstream: rdbuf()->sgetn(buf, rdbuf()->in_avail()) contentsdiffer from str()

W

whatdoineed2do

hi,

i'm trying avoid uncessary copy of data whilst using the stringstream
so i am using the sgetn() to copy the data into a buf as apposed to
getting the same data via the .str() which will construct a string
object, and a string object copy on return to me.

however i've noticed that if the last thing put onto the stringstream
is not txt (an int for example) then the sgetn() using the in_avail()
DIFFERS from what i get back from .str(). Below illustrates my
problem

Am using SunStudio 10 on sparc 5.10 -- haven't had chance to try g++

any thoughts on where i may be going wrong appreciated
thanks
ray


#include <unistd.h>

#include <iostream>
#include <sstream>
using namespace std;

int main()
{
stringstream s;

s.seekp(0); s.seekg(0);
s << "pid=" << getpid();
cout << "'" << s.str() << "'" << endl; // PID PRINTED AS EXPECTED


char buf[100];
memset(buf, 0, 100);

s.seekp(0); s.seekg(0);
s << "pid=" << getpid();
s.rdbuf()->sgetn(buf, s.rdbuf()->in_avail());
cout << "'" << buf << "'" << endl; // PID NOT PRINTED

memset(buf, 0, 100);

s.seekp(0); s.seekg(0);
s << "pid=" << getpid() << " and some text";
s.rdbuf()->sgetn(buf, s.rdbuf()->in_avail());
cout << "'" << buf << "'" << endl; // AS EXPECTED

return 0;
 
S

Steffen Sauer

hi,

i'm trying avoid uncessary copy of data whilst using the stringstream
so i am using the sgetn() to copy the data into a buf as apposed to
getting the same data via the .str() which will construct a string
object, and a string object copy on return to me.

Before doing such things you should really figure out if this is the
real bottleneck of your application. Often it is not the case...
however i've noticed that if the last thing put onto the stringstream
is not txt (an int for example) then the sgetn() using the in_avail()
DIFFERS from what i get back from .str(). Below illustrates my
problem

Am using SunStudio 10 on sparc 5.10 -- haven't had chance to try g++

any thoughts on where i may be going wrong appreciated
thanks
ray


#include <unistd.h>

#include <iostream>
#include <sstream>
using namespace std;

int main()
{
stringstream s;

s.seekp(0); s.seekg(0);
s << "pid=" << getpid();
cout << "'" << s.str() << "'" << endl; // PID PRINTED AS EXPECTED


char buf[100];
memset(buf, 0, 100);

s.seekp(0); s.seekg(0);
s << "pid=" << getpid();
s.rdbuf()->sgetn(buf, s.rdbuf()->in_avail());
cout << "'" << buf << "'" << endl; // PID NOT PRINTED

memset(buf, 0, 100);

s.seekp(0); s.seekg(0);
s << "pid=" << getpid() << " and some text";
s.rdbuf()->sgetn(buf, s.rdbuf()->in_avail());
cout << "'" << buf << "'" << endl; // AS EXPECTED

return 0;

The behaviour seems system dependent. On my VS2005 none of the sgetn()
methods from your example returned the complete string. However if the
passed array size to sgetn() is larger than the characters written to
the buffer all characters are returned.

memset(buf, 0, 100);
s.rdbuf()->sgetn(buf, 99);

Steffen.
 
W

whatdoineed2do

i'm trying avoid uncessary copy of data whilst using the stringstream
so i am using the sgetn() to copy the data into a buf as apposed to
getting the same data via the .str() which will construct a string
object, and a string object copy on return to me.

Before doing such things you should really figure out if this is the
real bottleneck of your application. Often it is not the case...




however i've noticed that if the last thing put onto the stringstream
is not txt (an int for example) then the sgetn() using the in_avail()
DIFFERS from what i get back from .str(). Below illustrates my
problem
Am using SunStudio 10 on sparc 5.10 -- haven't had chance to try g++
any thoughts on where i may be going wrong appreciated
thanks
ray
#include <unistd.h>
#include <iostream>
#include <sstream>
using namespace std;
int main()
{
stringstream s;
s.seekp(0); s.seekg(0);
s << "pid=" << getpid();
cout << "'" << s.str() << "'" << endl; // PID PRINTED AS EXPECTED
char buf[100];
memset(buf, 0, 100);
s.seekp(0); s.seekg(0);
s << "pid=" << getpid();
s.rdbuf()->sgetn(buf, s.rdbuf()->in_avail());
cout << "'" << buf << "'" << endl; // PID NOT PRINTED
memset(buf, 0, 100);
s.seekp(0); s.seekg(0);
s << "pid=" << getpid() << " and some text";
s.rdbuf()->sgetn(buf, s.rdbuf()->in_avail());
cout << "'" << buf << "'" << endl; // AS EXPECTED
return 0;

The behaviour seems system dependent. On my VS2005 none of the sgetn()
methods from your example returned the complete string. However if the
passed array size to sgetn() is larger than the characters written to
the buffer all characters are returned.

memset(buf, 0, 100);
s.rdbuf()->sgetn(buf, 99);

Steffen.

g++ (4.1.1 on x86 linux at least) also gives me odd results but i dont
know how complete their work is on the c++ std.

it would appear that maybe our understanding/implm of the stringbuf's
in_avail() isn't quite right.

if you see anything else on this it'd be great to hear about it
thanks
ray
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,968
Messages
2,570,150
Members
46,697
Latest member
AugustNabo

Latest Threads

Top