目前的地址 [英] current address

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

问题描述

为了在变量块的给定点标记当前地址,

我使用char dummy [0],如下所示:


int a;

long b;

short int c;

char dummy [0];


通过这种方式,我可以计算包含所有内存块的大小

vars:size = dummy - (char *)& a;


有没有其他方法可以做到这一点?比如


_bookmark1

int a;

long b;

short int c; < br $>
_bookmark2


size = bookmark2 - bookmark1;


谢谢,


Vu

解决方案

Vu Pham写道:

int a;
...
char dummy [0];


这是一个约束违规,你的编译器应该警告你。

通过这种方式,我可以计算包含所有内存块的大小
vars:size = dummy - (char *)&一个;


它可能适合你,但标准并不保证

这个陈述的结果或之后的任何结果。

有没有其他方法可以做到这一点?




不便携,虽然你可以通过放置

获得某种结果结构中的声明为字段(并采用结构的大小);我会b / b
表示你​​可能会错误地相信你会不会需要这样的功能。


-

++ acr @,ka"




" Sam Dennis" < sa*@malfunction.screaming.net>在消息中写道

news:sl **************** @ ID-227112.user.uni-berlin.de ...

Vu Pham写道:

int a;
...
char dummy [0];



这是一个约束违规,你的编译器应警告你。




是的,在一个编译器上,我得到一个警告,在另一个编译器上,我收到一个错误: - (

通过这种方式,我可以计算包含
所有变量的内存块的大小:size = dummy - (char *) & a;



它可能适合你,但标准并不保证
此声明的结果或之后的任何内容。
< blockquote class =post_quotes>有没有其他方法可以做到这一点?



不可移植,虽然你可以通过将
声明放在struct中来得到某种结果作为字段(并采取th的大小e struct);我会建议你可能会错误地相信你会需要这样的功能。




我在做的是将我的应用程序的当前状态保存到磁盘。而不是保存每个变量的
,我想将它们保存为一个连续的

块。我知道内存对齐可以使这个块具有不同大小的b $ b,但对于我的应用程序,这些保存的数据不会被移动到其他

平台而不会被重新创建。


在这种情况下,是否有任何关键字/方法...可以帮助我获得

当前地址如上所述?我可以声明另一个变量,比如char

dummy(而不是char dummy [0]),但是这将添加一个额外的变量

我的应用程序真的不需要。 />

谢谢,


Vu




" Vu Pham < vu@sivell.com>在消息中写道


我正在做的是将我的应用程序的当前状态保存到磁盘。
我想保存它们,而不是保存每个变量。 br />作为连续块。我知道内存对齐可以使这个块具有不同的大小,但是对于我的应用程序,这个保存的数据不会被移动到其他平台而不会被重新创建
correspnodingly。



C不是正确的语言。通常你的编译器会附带

文档,关于如何连接汇编程序,以及汇编

你可以很容易地找到堆栈(可能)并保存它,同样

重新加载时重新构建它。


In order to mark the current address at a given point of the variable block,
I use the char dummy[0] as follows :

int a;
long b;
short int c;
char dummy[0];

By this way, I can calculate the size of the memory block that contains all
the vars : size = dummy - ( char *) & a;

Is there any other way to do this ? Such as

_bookmark1
int a;
long b;
short int c;
_bookmark2

size = bookmark2 - bookmark1;

Thanks,

Vu

解决方案

Vu Pham wrote:

int a;
...
char dummy[0];
This is a constraint violation and your compiler should warn you of it.
By this way, I can calculate the size of the memory block that contains all
the vars : size = dummy - ( char *) & a;
It might work for you, but the standard doesn''t guarantee anything about
the result of this statement or anything thereafter.
Is there any other way to do this ?



Not portably, although you could get some sort of result by placing your
declaration in a struct as fields (and taking the size of the struct); I
would suggest that you are probably mistaken in believing that you would
ever need such functionality, though.

--
++acr@,ka"



"Sam Dennis" <sa*@malfunction.screaming.net> wrote in message
news:sl****************@ID-227112.user.uni-berlin.de...

Vu Pham wrote:

int a;
...
char dummy[0];



This is a constraint violation and your compiler should warn you of it.



Yes, on one compiler, I get a warning, on another one, I get an error :-(

By this way, I can calculate the size of the memory block that contains all the vars : size = dummy - ( char *) & a;



It might work for you, but the standard doesn''t guarantee anything about
the result of this statement or anything thereafter.

Is there any other way to do this ?



Not portably, although you could get some sort of result by placing your
declaration in a struct as fields (and taking the size of the struct); I
would suggest that you are probably mistaken in believing that you would
ever need such functionality, though.



What I am doing is to save the current status of my app to disk. Instead of
saving each of these variables, I would like to save them as a continuos
block. I understand that memory alignment can make this block to have
different size but , for my app, this saved data will not be moved to other
platforms without being recreated correspnodingly.

In such case, is there any keyword/method ... could help me to get the
current address as described ? I can declare another variable like char
dummy ( instead of char dummy[0] ) but this will add an extra variable that
my app really does not need.

Thanks,

Vu



"Vu Pham" <vu@sivell.com> wrote in message


What I am doing is to save the current status of my app to disk.
Instead of saving each of these variables, I would like to save them
as a continuos block. I understand that memory alignment can
make this block to have different size but , for my app, this saved
data will not be moved to other platforms without being recreated
correspnodingly.


C isn''t the right language for this. Normally your compiler will come with
documentation about how to interface to assembly programs, and from assembly
you can find the stack quite easily (probably) and save it, also
reconstitute it on reload.


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

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