结构尺寸 [英] Structure size

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

问题描述

我想分配一个1024字节的结构大小,但我希望

编译器为我做计算。


typedef struct

{

int var1;

int var2;

int var3;

char var4 [?????? ];

} MYSTRUCT;


我想要做的就是更换??????自动生成的东西
使总结构为1024字节,而不必自己手动计算其他成员的
字节。


这可能用c ++吗?


谢谢。


布鲁斯。

解决方案
布鲁斯。说:


我想分配一个1024字节的结构大小,但我希望

编译器为我做计算。 br />

typedef struct

{

int var1;

int var2;

int var3;

char var4 [?????? ];

} MYSTRUCT;


我想要做的就是更换??????什么东西会自动地使总结构为1024字节,而不必自己手动计算其他成员的字节数。


这可能在c ++中吗?



不可移植。实现可以在任何

结构成员之后插入任意填充。在病态实现中,您可能需要

给var4一个负数元素(当然这不可能是
)。


如果你需要正好1024个字节,请分配一个unsigned char数组,1024
字节宽,并在需要时将其放入其位。


-

Richard Heathfield< http://www.cpax.org.uk>

电邮:-www。 + rjh @

谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>

Usenet是一个奇怪的放置" - dmr 1999年7月29日


2007-07-19 17:39,布鲁斯。写道:


我想分配1024字节的结构大小,但我希望

编译器为我做计算。 br />

typedef struct

{

int var1;

int var2;

int var3;

char var4 [?????? ];

} MYSTRUCT;


我想要做的就是更换??????自动生成的东西
使总结构为1024字节,而不必自己手动计算其他成员的
字节。



你可以这样做:


struct TheStruct {

int var1;

int var2;

int var3;

char var4 [1024 - (3 * sizeof(int))];

};


(另请注意缺少typedef,除非您需要C

兼容性,否则不需要它。)


-

Erik Wikstr?m


Erik Wikstr?m说:


2007-07-19 17:39,布鲁斯。写道:


>我想分配1024字节的结构大小但是我想让编译器为我做计算。

typedef struct
{int = var int;
int var2;
int var3;
char var4 [?????? MYSTRUCT;

我想要做的就是更换??????将自动地使总结构为1024字节而不必自己手动计算其他成员的字节数的东西。



您可以这样做:


struct TheStruct {

int var1;

int var2;

int var3;

char var4 [1024 - (3 * sizeof(int))];

};



它甚至可能有用。但它可能不会。这取决于

的实现。


(注意缺少typedef,除非你需要C
兼容性)。



它在C中实际上并不需要,但不可否认的是它的遗漏

确实导致了更多的代码。
< br $>
-

Richard Heathfield< http://www.cpax.org.uk>

电子邮件:-www。 + rjh @

谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>

Usenet是一个奇怪的放置" - dmr 1999年7月29日


I would like to allocate a structure size of 1024 bytes but I want the
compiler to do the calculation for me.

typedef struct
{
int var1;
int var2;
int var3;
char var4[ ?????? ];
} MYSTRUCT;

What I want to do is replace the ?????? something that will automattically
make the total structure 1024 bytes without having to manually count the
bytes of the other members myself.

Is that possible in c++?

Thanks.

Bruce.

解决方案

Bruce. said:

I would like to allocate a structure size of 1024 bytes but I want the
compiler to do the calculation for me.

typedef struct
{
int var1;
int var2;
int var3;
char var4[ ?????? ];
} MYSTRUCT;

What I want to do is replace the ?????? something that will
automattically make the total structure 1024 bytes without having to
manually count the bytes of the other members myself.

Is that possible in c++?

Not portably. Implementations may insert arbitrary padding after any
structure member. In a pathological implementation, you might have to
give var4 a negative number of elements (which is of course
impossible).

If you need exactly 1024 bytes, allocate an array of unsigned char, 1024
bytes wide, and dip into its bits as and when you need to.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999


On 2007-07-19 17:39, Bruce. wrote:

I would like to allocate a structure size of 1024 bytes but I want the
compiler to do the calculation for me.

typedef struct
{
int var1;
int var2;
int var3;
char var4[ ?????? ];
} MYSTRUCT;

What I want to do is replace the ?????? something that will automattically
make the total structure 1024 bytes without having to manually count the
bytes of the other members myself.

You could do something like this:

struct TheStruct {
int var1;
int var2;
int var3;
char var4[1024 - (3 * sizeof(int))];
};

(Notice also the lack of typedef, it''s not needed unless you require C
compatibility).

--
Erik Wikstr?m


Erik Wikstr?m said:

On 2007-07-19 17:39, Bruce. wrote:

>I would like to allocate a structure size of 1024 bytes but I want
the compiler to do the calculation for me.

typedef struct
{
int var1;
int var2;
int var3;
char var4[ ?????? ];
} MYSTRUCT;

What I want to do is replace the ?????? something that will
automattically make the total structure 1024 bytes without having to
manually count the bytes of the other members myself.


You could do something like this:

struct TheStruct {
int var1;
int var2;
int var3;
char var4[1024 - (3 * sizeof(int))];
};

And it might even work. But it might not. It depends on the
implementation.

(Notice also the lack of typedef, it''s not needed unless you require C
compatibility).

It''s not actually needed in C either, although admittedly its omission
does lead to wordier code.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999


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

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