c ++流到内存地址 [英] c++ stream to memory address

查看:59
本文介绍了c ++流到内存地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一些棘手的代码并需要专家的帮助。


我有几个必须写的大型数据结构(uBLAS矩阵)

到预分配(由另一个程序)的静态内存块。目前

我们的代码使用BOOST :: serialize存档模拟此行为。根据文档中的
,这些归档对象将写入给定的ostream。

我们想要定义一个写入此地址的ostream,而不是

将这些大型数据结构复制到缓冲区。


我对任何优雅的解决方案(如果存在)都开放,但我们目前正在试图尝试将给予我们的内存设置为ostream使用的缓冲区。

再次,如果有更好的方法,我们很乐意使用它。我提供

以上信息,以强调我们非常依赖于使用c ++流

我们的i / o与静态内存。

下面是一个*简单的例子,我无法开始工作。我在

AIX上使用vacpp。


提前感谢您提供的任何帮助。


- dan


struct X

{

短垫;

短版;

int x;

int y;

int z;

};

int main() {


X dummy;

dummy.x = 5;

dummy.y = 5;

dummy.z = 5;

短a = 20;


cout<< 假人的大小是 << 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()<< endl;


ofs<< a<< 21;


cout<< dummy.version<< " " << dummy.x<< " " << dummy.y<< " " <<

dummy.z<< endl;

}

解决方案



" Dan Elliott" <哒************************ @ noSpam.org>在留言中写道

新闻:5h *********** @ dfw-service2.ext.ray.com ...

下面是*简单*我不能上班的例子。我在AIX上使用vacpp


提前感谢您提供的任何帮助。

- dan

struct X
{
短垫;
短版;
int x;
int y;
int z;
};

int main(){x dummy;
dummy.x = 5;
dummy.y = 5;
dummy.z = 5;
短a = 20;

cout<< 假人的大小是 << sizeof(dummy)<< endbu;

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;

ostreams(tempBuf);

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

ofs<< a<< 21;

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




以下是此示例程序的输出:


dummyof dummy是16

pubsetbuf:11001d470 vs 11001d470

dummy.version addr:fffffffffffe662

dummy addr:fffffffffffe660

ofs good?: 1

0 5 5 5

0 5 5 5


我做错了什么?


" Dan Elliott"写了

我正在研究一些棘手的代码,需要专家的帮助。

我有几个必须写的大型数据结构(uBLAS矩阵)
预先分配(由另一个程序)的静态内存块。目前我们的代码使用BOOST :: serialize存档模拟此行为。根据文档说明,这些归档对象将写入给定的ostream。
我们希望定义一个写入此地址的ostream,而不将这些大型数据结构复制到缓冲区。我对任何优雅的解决方案(如果存在)都持开放态度,但我们目前正在尝试将给予我们的内存设置为ostream使用的缓冲区。
同样,如果有更好的方法,我们很乐意使用它。我提供上面的信息,以强调我们非常依赖于使用c ++流来支持我的静态内存i / o。


尝试这种方法:


//使用ostrstream写入内存:

char szBuf [1024] = ";

ostrstream os(szBuf,sizeof(szBuf));

os<< test data \\\


<< 更多测试数据\ n;


下面是一个*简单*示例,我无法开始工作。我在AIX上使用vacpp。

提前感谢您提供的任何帮助。

- dan

struct X
{
短垫;
短版;
int x;
int y;
int z;
};

int main(){x dummy;
dummy.x = 5;
dummy.y = 5;
dummy.z = 5;
短a = 20;

cout<< 假人的大小是 << sizeof(dummy)<< endbu;

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;

ostreams(tempBuf);

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

ofs<< a<< 21;

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



" Dan Elliott" <哒************************ @ noSpam.org>在留言中写道

新闻:5h *********** @ dfw-service2.ext.ray.com ...


* snip *

我在AIX上使用vacpp。



这是另一个我认为应该的代码示例(但不是)使用

预先分配的内存作为iostream(或ostream)对象的缓冲区:


char szBuf [1024] =" test data \ nmore测试数据\ n" ;;

stringbuf myStringBuf;

myStringBuf.pubsetbuf(szBuf,1024);

iostream myIostream(& myStringBuf) );

myIostream<< 你好,你好吗?你在做什么;


szBuf指向的内存未被iostream

操作修改。我在这里不知所措!


- dan


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" <da************************@noSpam.org> wrote in message
news:5h***********@dfw-service2.ext.ray.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_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;
}



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?


"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 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::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" <da************************@noSpam.org> wrote in message
news:5h***********@dfw-service2.ext.ray.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.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


这篇关于c ++流到内存地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆