Connecting Tech Pros Worldwide Help | Site Map

c++ stream to memory address

Dan Elliott
Guest
 
Posts: n/a
#1: Jul 23 '05
I am working on some tricky code and need some help from the experts.

I have several large data structures (uBLAS matrices) that must be written
to a pre-allocated (by another program) chunk of static memory. Currently
our code emulates this behavior using BOOST::serialize archives. According
to the documentation, these archive objects will write to a given ostream.
We would like to define an ostream that writes to this address without
copying these large data structures to a buffer.

I am open to any elegant solution (if one exists), but we are currently
attempting to set the memory given to us as the buffer used by an ostream.
Again, if there is a better way, we would gladly use it. I am providing the
above information to emphasize that we are pretty tied to using c++ streams
for our i/o with the static memory.

Below is a *simple* example that I cannot get to work. I am using vacpp on
AIX.

Thank you in advance for any help provided.

- dan

struct X
{
short pad;
short version;
int x;
int y;
int z;
};


int main() {

X dummy;
dummy.x = 5;
dummy.y = 5;
dummy.z = 5;
short a = 20;

cout << "The sizeof dummy is " << sizeof(dummy) << endl;

stringbuf *tempBuf = new stringbuf(ios_base::out|ios_base::binary);
streambuf *testBuf =
tempBuf->pubsetbuf((char*)(&(dummy.version)),sizeof(dumm y)-2);

cout << "pubsetbuf: " << (void*)testBuf << " vs " << (void*)tempBuf <<
endl;

ostream ofs(tempBuf);

cout << "dummy.version addr: " << &(dummy.version) << endl <<"dummy
addr: " << &(dummy) << endl << "ofs good?: " << ofs.good() << endl;

ofs << a << 21;

cout << dummy.version << " " << dummy.x << " " << dummy.y << " " <<
dummy.z << endl;
}


Dan Elliott
Guest
 
Posts: n/a
#2: Jul 23 '05

re: c++ stream to memory address



"Dan Elliott" <dan_elliott_at_cox_dot_net@noSpam.org> wrote in message
news:5hace.7$134.4@dfw-service2.ext.ray.com...[color=blue]
> Below is a *simple* example that I cannot get to work. I am using vacpp[/color]
on[color=blue]
> AIX.
>
> Thank you in advance for any help provided.
>
> - dan
>
> struct X
> {
> short pad;
> short version;
> int x;
> int y;
> int z;
> };
>
>
> int main() {
>
> X dummy;
> dummy.x = 5;
> dummy.y = 5;
> dummy.z = 5;
> short a = 20;
>
> cout << "The sizeof dummy is " << sizeof(dummy) << endl;
>
> stringbuf *tempBuf = new stringbuf(ios_base::out|ios_base::binary);
> streambuf *testBuf =
> tempBuf->pubsetbuf((char*)(&(dummy.version)),sizeof(dumm y)-2);
>
> cout << "pubsetbuf: " << (void*)testBuf << " vs " << (void*)tempBuf <<
> endl;
>
> ostream ofs(tempBuf);
>
> cout << "dummy.version addr: " << &(dummy.version) << endl <<"dummy
> addr: " << &(dummy) << endl << "ofs good?: " << ofs.good() << endl;
>
> ofs << a << 21;
>
> cout << dummy.version << " " << dummy.x << " " << dummy.y << " " <<
> dummy.z << endl;
> }[/color]

Here is the output from this example program:

The sizeof dummy is 16
pubsetbuf: 11001d470 vs 11001d470
dummy.version addr: fffffffffffe662
dummy addr: fffffffffffe660
ofs good?: 1
0 5 5 5
0 5 5 5

What am I doing wrong?


Uenal Mutlu
Guest
 
Posts: n/a
#3: Jul 23 '05

re: c++ stream to memory address


"Dan Elliott" wrote[color=blue]
> I am working on some tricky code and need some help from the experts.
>
> I have several large data structures (uBLAS matrices) that must be written
> to a pre-allocated (by another program) chunk of static memory. Currently
> our code emulates this behavior using BOOST::serialize archives. According
> to the documentation, these archive objects will write to a given ostream.
> We would like to define an ostream that writes to this address without
> copying these large data structures to a buffer.
>
> I am open to any elegant solution (if one exists), but we are currently
> attempting to set the memory given to us as the buffer used by an ostream.
> Again, if there is a better way, we would gladly use it. I am providing the
> above information to emphasize that we are pretty tied to using c++ streams
> for our i/o with the static memory.[/color]

Try this method:

// writing to memory using ostrstream:
char szBuf[1024] = "";
ostrstream os(szBuf, sizeof(szBuf));
os << "test data\n"
<< "more test data\n";



[color=blue]
> Below is a *simple* example that I cannot get to work. I am using vacpp on
> AIX.
>
> Thank you in advance for any help provided.
>
> - dan
>
> struct X
> {
> short pad;
> short version;
> int x;
> int y;
> int z;
> };
>
>
> int main() {
>
> X dummy;
> dummy.x = 5;
> dummy.y = 5;
> dummy.z = 5;
> short a = 20;
>
> cout << "The sizeof dummy is " << sizeof(dummy) << endl;
>
> stringbuf *tempBuf = new stringbuf(ios_base::out|ios_base::binary);
> streambuf *testBuf =
> tempBuf->pubsetbuf((char*)(&(dummy.version)),sizeof(dumm y)-2);
>
> cout << "pubsetbuf: " << (void*)testBuf << " vs " << (void*)tempBuf <<
> endl;
>
> ostream ofs(tempBuf);
>
> cout << "dummy.version addr: " << &(dummy.version) << endl <<"dummy
> addr: " << &(dummy) << endl << "ofs good?: " << ofs.good() << endl;
>
> ofs << a << 21;
>
> cout << dummy.version << " " << dummy.x << " " << dummy.y << " " <<
> dummy.z << endl;
> }[/color]


Dan Elliott
Guest
 
Posts: n/a
#4: Jul 23 '05

re: c++ stream to memory address


"Dan Elliott" <dan_elliott_at_cox_dot_net@noSpam.org> wrote in message
news:5hace.7$134.4@dfw-service2.ext.ray.com...

*snip*
[color=blue]
> I am using vacpp on AIX.[/color]


Here is another example of code that I feel should (but doesn't) use
pre-allocated memory as the buffer for an iostream (or ostream) object:

char szBuf[1024] = "test data\nmore test data\n";
stringbuf myStringBuf;
myStringBuf.pubsetbuf(szBuf,1024);
iostream myIostream(&myStringBuf);
myIostream << "hello hi\nhow are you doing\n";

The memory pointed to by szBuf has not been modified by the iostream
operation. I am at a loss here!

- dan


Dan Elliott
Guest
 
Posts: n/a
#5: Jul 23 '05

re: c++ stream to memory address


Thank you for the reply.

I am under the impression that ostrstream is no longer a part of the
standard. If so, it cannot be used on this project.

Thank you,

dan

"Uenal Mutlu" <520001085531-0001@t-online.de> wrote in message
news:d4rmr9$nrp$01$1@news.t-online.com...[color=blue]
> "Dan Elliott" wrote[color=green]
> > I am working on some tricky code and need some help from the experts.
> >
> > I have several large data structures (uBLAS matrices) that must be[/color][/color]
written[color=blue][color=green]
> > to a pre-allocated (by another program) chunk of static memory.[/color][/color]
Currently[color=blue][color=green]
> > our code emulates this behavior using BOOST::serialize archives.[/color][/color]
According[color=blue][color=green]
> > to the documentation, these archive objects will write to a given[/color][/color]
ostream.[color=blue][color=green]
> > We would like to define an ostream that writes to this address without
> > copying these large data structures to a buffer.
> >
> > I am open to any elegant solution (if one exists), but we are currently
> > attempting to set the memory given to us as the buffer used by an[/color][/color]
ostream.[color=blue][color=green]
> > Again, if there is a better way, we would gladly use it. I am providing[/color][/color]
the[color=blue][color=green]
> > above information to emphasize that we are pretty tied to using c++[/color][/color]
streams[color=blue][color=green]
> > for our i/o with the static memory.[/color]
>
> Try this method:
>
> // writing to memory using ostrstream:
> char szBuf[1024] = "";
> ostrstream os(szBuf, sizeof(szBuf));
> os << "test data\n"
> << "more test data\n";
>
>
>
>[color=green]
> > Below is a *simple* example that I cannot get to work. I am using vacpp[/color][/color]
on[color=blue][color=green]
> > AIX.
> >
> > Thank you in advance for any help provided.
> >
> > - dan
> >
> > struct X
> > {
> > short pad;
> > short version;
> > int x;
> > int y;
> > int z;
> > };
> >
> >
> > int main() {
> >
> > X dummy;
> > dummy.x = 5;
> > dummy.y = 5;
> > dummy.z = 5;
> > short a = 20;
> >
> > cout << "The sizeof dummy is " << sizeof(dummy) << endl;
> >
> > stringbuf *tempBuf = new stringbuf(ios_base::out|ios_base::binary);
> > streambuf *testBuf =
> > tempBuf->pubsetbuf((char*)(&(dummy.version)),sizeof(dumm y)-2);
> >
> > cout << "pubsetbuf: " << (void*)testBuf << " vs " << (void*)tempBuf[/color][/color]
<<[color=blue][color=green]
> > endl;
> >
> > ostream ofs(tempBuf);
> >
> > cout << "dummy.version addr: " << &(dummy.version) << endl <<"dummy
> > addr: " << &(dummy) << endl << "ofs good?: " << ofs.good() << endl;
> >
> > ofs << a << 21;
> >
> > cout << dummy.version << " " << dummy.x << " " << dummy.y << " " <<
> > dummy.z << endl;
> > }[/color]
>
>[/color]


red floyd
Guest
 
Posts: n/a
#6: Jul 23 '05

re: c++ stream to memory address


Dan Elliott wrote:[color=blue]
> Thank you for the reply.
>
> I am under the impression that ostrstream is no longer a part of the
> standard. If so, it cannot be used on this project.
>
> Thank you,
>
> dan[/color]
[redacted]

True, but std::ostringstream *is* part of the standard.

#include <sstream>
Default User
Guest
 
Posts: n/a
#7: Jul 23 '05

re: c++ stream to memory address



red floyd wrote:[color=blue]
> Dan Elliott wrote:[color=green]
> > Thank you for the reply.
> >
> > I am under the impression that ostrstream is no longer a part of[/color][/color]
the[color=blue][color=green]
> > standard. If so, it cannot be used on this project.
> >
> > Thank you,
> >
> > dan[/color]
> [redacted]
>
> True,[/color]

No, not true. The strstream stuff is deprecated. It's still available
and probably will be for some time to come.

One definitely should try to avoid it in new code, of course.
[color=blue]
> but std::ostringstream *is* part of the standard.[/color]

Yes, this would be the better choice.



Brian

Dan Elliott
Guest
 
Posts: n/a
#8: Jul 23 '05

re: c++ stream to memory address


"red floyd" <no.spam@here.dude> wrote in message
news:h8yde.1129$5o2.830@newssvr13.news.prodigy.com ...[color=blue]
> Dan Elliott wrote:[color=green]
> > I am under the impression that ostrstream is no longer a part of the
> > standard. If so, it cannot be used on this project.
> >
> > Thank you,
> >
> > dan[/color]
> [redacted]
>
> True, but std::ostringstream *is* part of the standard.
>
> #include <sstream>[/color]

Great, but I do not see a constructor that will allow me to do what my
original post was asking for: I want to write to a pre-allocated memory
address. I am writing a lot of data to this address, so I do not want to
use a stream that will force me to copy to memory controlled (and
untouchable by me) by the stream and then copy to the pre-allocated memory.

In addition, I have tried directing the stringbuf object belonging to a
ostringstream to use the pre-allocated memory with negative results. The
code I have posted will show an example of what I have tried.

Thanks for your help. I am totally clueless at this point.

- dan


Ron Natalie
Guest
 
Posts: n/a
#9: Jul 23 '05

re: c++ stream to memory address


Dan Elliott wrote:
[color=blue]
>
> Great, but I do not see a constructor that will allow me to do what my
> original post was asking for: I want to write to a pre-allocated memory
> address.[/color]
Use the deprecated strstream interface.
It writes to a managed hunka-char array rather than a string object.
Alf P. Steinbach
Guest
 
Posts: n/a
#10: Jul 23 '05

re: c++ stream to memory address


* Dan Elliott:[color=blue]
> I am working on some tricky code and need some help from the experts.
>
> I have several large data structures (uBLAS matrices) that must be written
> to a pre-allocated (by another program) chunk of static memory. Currently
> our code emulates this behavior using BOOST::serialize archives. According
> to the documentation, these archive objects will write to a given ostream.
> We would like to define an ostream that writes to this address without
> copying these large data structures to a buffer.
>
> I am open to any elegant solution (if one exists), but we are currently
> attempting to set the memory given to us as the buffer used by an ostream.
> Again, if there is a better way, we would gladly use it. I am providing the
> above information to emphasize that we are pretty tied to using c++ streams
> for our i/o with the static memory.
>
> Below is a *simple* example that I cannot get to work. I am using vacpp on
> AIX.
>
> Thank you in advance for any help provided.
>
> - dan
>
> struct X
> {
> short pad;
> short version;
> int x;
> int y;
> int z;
> };
>
>
> int main() {
>
> X dummy;
> dummy.x = 5;
> dummy.y = 5;
> dummy.z = 5;
> short a = 20;
>
> cout << "The sizeof dummy is " << sizeof(dummy) << endl;
>
> stringbuf *tempBuf = new stringbuf(ios_base::out|ios_base::binary);
> streambuf *testBuf =
> tempBuf->pubsetbuf((char*)(&(dummy.version)),sizeof(dumm y)-2);
>
> cout << "pubsetbuf: " << (void*)testBuf << " vs " << (void*)tempBuf <<
> endl;
>
> ostream ofs(tempBuf);
>
> cout << "dummy.version addr: " << &(dummy.version) << endl <<"dummy
> addr: " << &(dummy) << endl << "ofs good?: " << ofs.good() << endl;
>
> ofs << a << 21;[/color]

Here you're converting the integer value 20 (of 'a') to character codes.


[color=blue]
> cout << dummy.version << " " << dummy.x << " " << dummy.y << " " <<
> dummy.z << endl;
> }[/color]

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jeff Flinn
Guest
 
Posts: n/a
#11: Jul 23 '05

re: c++ stream to memory address



"Dan Elliott" <dan_elliott_at_cox_dot_net@noSpam.org> wrote in message
news:5hace.7$134.4@dfw-service2.ext.ray.com...[color=blue]
>I am working on some tricky code and need some help from the experts.
>
> I have several large data structures (uBLAS matrices) that must be written
> to a pre-allocated (by another program) chunk of static memory. Currently
> our code emulates this behavior using BOOST::serialize archives.
> According
> to the documentation, these archive objects will write to a given ostream.
> We would like to define an ostream that writes to this address without
> copying these large data structures to a buffer.[/color]

Have you looked at using boost::iostream library from Jonathan Turkanis? I
use it along with boost::serialization to stream data to/from the Windows
clipboard allocated memory. IIRC, it will be part of boost 1.33.0. It is
also available for use from the files section at www.boost.org.

Look for:

boost::io::array_sink
boost::io::array_source

which take (const) char*'s and direct input/output from/to externally
allocated memory.

You might want to join the boost devel and/or user mailing lists to get more
immediate response to these sorts of inquiries.

Jeff Flinn


Closed Thread