473,659 Members | 2,683 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

c++ stream to memory address

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::serializ e 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_b ase::out|ios_ba se::binary);
streambuf *testBuf =
tempBuf->pubsetbuf((cha r*)(&(dummy.ver sion)),sizeof(d ummy)-2);

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

ostream ofs(tempBuf);

cout << "dummy.vers ion 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;
}
Jul 23 '05 #1
10 5765

"Dan Elliott" <da************ ************@no Spam.org> wrote in message
news:5h******** ***@dfw-service2.ext.ra y.com...
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_b ase::out|ios_ba se::binary);
streambuf *testBuf =
tempBuf->pubsetbuf((cha r*)(&(dummy.ver sion)),sizeof(d ummy)-2);

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

ostream ofs(tempBuf);

cout << "dummy.vers ion 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?
Jul 23 '05 #2
"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::serializ e 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_b ase::out|ios_ba se::binary);
streambuf *testBuf =
tempBuf->pubsetbuf((cha r*)(&(dummy.ver sion)),sizeof(d ummy)-2);

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

ostream ofs(tempBuf);

cout << "dummy.vers ion 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;
}

Jul 23 '05 #3
"Dan Elliott" <da************ ************@no Spam.org> wrote in message
news:5h******** ***@dfw-service2.ext.ra y.com...

*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.pub setbuf(szBuf,10 24);
iostream myIostream(&myS tringBuf);
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
Jul 23 '05 #4
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" <52************ ***@t-online.de> wrote in message
news:d4******** *****@news.t-online.com...
"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::serializ e 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_b ase::out|ios_ba se::binary);
streambuf *testBuf =
tempBuf->pubsetbuf((cha r*)(&(dummy.ver sion)),sizeof(d ummy)-2);

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

ostream ofs(tempBuf);

cout << "dummy.vers ion 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;
}


Jul 23 '05 #5
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, but std::ostringstr eam *is* part of the standard.

#include <sstream>
Jul 23 '05 #6

red floyd wrote:
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.
but std::ostringstr eam *is* part of the standard.


Yes, this would be the better choice.

Brian

Jul 23 '05 #7
"red floyd" <no*****@here.d ude> wrote in message
news:h8******** ********@newssv r13.news.prodig y.com...
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::ostringstr eam *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
Jul 23 '05 #8
Dan Elliott wrote:

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.
Jul 23 '05 #9
* 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::serializ e 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_b ase::out|ios_ba se::binary);
streambuf *testBuf =
tempBuf->pubsetbuf((cha r*)(&(dummy.ver sion)),sizeof(d ummy)-2);

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

ostream ofs(tempBuf);

cout << "dummy.vers ion 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.
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?
Jul 23 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
1726
by: Jason | last post by:
How can I create a stream object from webservice that returns an image in byte format?
6
3697
by: Ian Robertson | last post by:
I am trying to write a function that takes a reference to an object as its arguement. The object is created in another function and I am trying to pass the object to another function from within the function that created the object. I have cut out some code as I dont think showing a for loop is helpful. void TMP3_Main_Form::LoadMP3() { TMemoryStream *MP3Stream = new TMemoryStream();...
3
15298
by: MJB | last post by:
I'm getting an IStream back from function xmlHttp.responsestream. I would like to convert this to a System.IO.Stream in order to work with it in my application. Has anyone encountered this and written a conversion? TIA, Matt
4
26795
by: TT (Tom Tempelaere) | last post by:
Hey there, I need a string stream, but I can't find one in .NET. I thought StringWriter would derive from Stream, alas it doesn't do so. Which leads me to my next question: What is the purpose of System.IO.StringWriter (or, what is its added value)??? It can do the same things with StringBuilder, and I see no added value. The reason why I need the string stream, is that I have a library that knows
3
1665
by: Marc Thompson | last post by:
Hi there, I have the following: Dim objResponse As WebResponse = objReq.GetResponse Dim objStream As IO.Stream objStream = objResponse.GetResponseStream Then, I read some Bytes from this stream and it works. I then go do some
4
2328
by: Scott F. Brown | last post by:
Greetings all... I was playing around with compressing streams and came across a behavior that I do not understand. I create a stream (input) from the contents of a textbox. That stream is compressed into another stream (output). I then copy the stream (output) to another stream (input2). The compressed stream (input2) is then decompressed into a final stream (output2). My question is this. I create input2 from output.GetBuffer()...
7
9880
by: iporter | last post by:
I use the code below to authorise the download of certain files. Thus, instead of linking to the file in a wwwroot directory, I link to this code with the filename as a parameter, and the script streams the file if the user is authorised. This has worked fine on PDFs, DOCs, XLS, etc. until today, and 18MB file presents the error message 'format error: not a pdf or corrupt'. Is there a file size limit, or a default that needs...
4
6403
by: rakesh.usenet | last post by:
For a particular application of mine - I need a simulation of byte array output stream. * write data onto a stream * getback the contiguous content as an array later for network transport. My code looks as follows. #include <iostream>
7
1960
by: mathieu | last post by:
Hi there, I am trying to rewrite this very slow function (due to AFAIK the extra copy): void DoAction(std::istream &is, std::ostream &os) { uint16_t c; while( is.read((char*)&c,2) )
0
8427
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8332
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8851
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8746
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8627
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4335
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2750
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.