C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

c++ stream to memory address

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Dan Elliott
Guest





PostPosted: Thu Apr 28, 2005 6:53 pm    Post subject: c++ stream to memory address Reply with quote



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(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;
}


Back to top
Dan Elliott
Guest





PostPosted: Thu Apr 28, 2005 10:07 pm    Post subject: Re: c++ stream to memory address Reply with quote




"Dan Elliott" <dan_elliott_at_cox_dot_net (AT) noSpam (DOT) org> wrote

Quote:
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(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?



Back to top
Uenal Mutlu
Guest





PostPosted: Thu Apr 28, 2005 10:12 pm    Post subject: Re: c++ stream to memory address Reply with quote



"Dan Elliott" wrote
Quote:
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 datan"
<< "more test datan";




Quote:
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(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;
}



Back to top
Dan Elliott
Guest





PostPosted: Mon May 02, 2005 5:36 pm    Post subject: Re: c++ stream to memory address Reply with quote

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 (AT) t-online (DOT) de> wrote

Quote:
"Dan Elliott" wrote
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 datan"
"more test datan";




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(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;
}





Back to top
Dan Elliott
Guest





PostPosted: Mon May 02, 2005 5:44 pm    Post subject: Re: c++ stream to memory address - another frustrating examp Reply with quote

"Dan Elliott" <dan_elliott_at_cox_dot_net (AT) noSpam (DOT) org> wrote


*snip*

Quote:
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 datanmore test datan";
stringbuf myStringBuf;
myStringBuf.pubsetbuf(szBuf,1024);
iostream myIostream(&myStringBuf);
myIostream << "hello hinhow are you doingn";

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

- dan



Back to top
red floyd
Guest





PostPosted: Mon May 02, 2005 10:49 pm    Post subject: Re: c++ stream to memory address Reply with quote

Dan Elliott wrote:
Quote:
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::ostringstream *is* part of the standard.

#include <sstream>

Back to top
Default User
Guest





PostPosted: Mon May 02, 2005 11:20 pm    Post subject: Re: c++ stream to memory address Reply with quote


red floyd wrote:
Quote:
Dan Elliott wrote:
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.

Quote:
but std::ostringstream *is* part of the standard.

Yes, this would be the better choice.



Brian


Back to top
Dan Elliott
Guest





PostPosted: Mon May 09, 2005 12:58 pm    Post subject: Re: c++ stream to memory address Reply with quote

"red floyd" <no.spam (AT) here (DOT) dude> wrote

Quote:
Dan Elliott wrote:
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::ostringstream *is* part of the standard.

#include

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



Back to top
Ron Natalie
Guest





PostPosted: Mon May 09, 2005 9:15 pm    Post subject: Re: c++ stream to memory address Reply with quote

Dan Elliott wrote:

Quote:

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.

Back to top
Alf P. Steinbach
Guest





PostPosted: Mon May 09, 2005 9:24 pm    Post subject: Re: c++ stream to memory address Reply with quote

* Dan Elliott:
Quote:
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(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.



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

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

Back to top
Jeff Flinn
Guest





PostPosted: Tue May 10, 2005 12:36 pm    Post subject: Re: c++ stream to memory address Reply with quote


"Dan Elliott" <dan_elliott_at_cox_dot_net (AT) noSpam (DOT) org> wrote

Quote:
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



Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.