c++ stream to memory address

D

Dan Elliott

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::eek:ut|ios_base::binary);
streambuf *testBuf =
tempBuf->pubsetbuf((char*)(&(dummy.version)),sizeof(dummy)-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;
}
 
D

Dan Elliott

Dan Elliott said:
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::eek:ut|ios_base::binary);
streambuf *testBuf =
tempBuf->pubsetbuf((char*)(&(dummy.version)),sizeof(dummy)-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;
}

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?
 
U

Uenal Mutlu

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.

Try this method:

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

Dan Elliott

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 said:
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.

Try this method:

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



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::eek:ut|ios_base::binary);
streambuf *testBuf =
tempBuf->pubsetbuf((char*)(&(dummy.version)),sizeof(dummy)-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;
}
 
D

Dan Elliott

*snip*
I am using vacpp on AIX.


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
 
R

red floyd

Dan said:
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
[redacted]

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

#include <sstream>
 
D

Default User

red said:
Dan said:
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
[redacted]

True,

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.
but std::eek:stringstream *is* part of the standard.

Yes, this would be the better choice.



Brian
 
D

Dan Elliott

red floyd said:
Dan said:
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
[redacted]

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

#include <sstream>

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
 
R

Ron Natalie

Dan said:
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.
Use the deprecated strstream interface.
It writes to a managed hunka-char array rather than a string object.
 
A

Alf P. Steinbach

* Dan Elliott:
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::eek:ut|ios_base::binary);
streambuf *testBuf =
tempBuf->pubsetbuf((char*)(&(dummy.version)),sizeof(dummy)-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;

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

Jeff Flinn

Dan Elliott said:
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.

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
 

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

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top