联盟的规模 [英] sizeof a union
问题描述
我的朋友问了我一个问题如下:
给工会SU定义为:
typedef union _SU
{
短x;
struct y {
char a;
短b;
char c;
};
} SU;
sizeof(SU)是什么?
我的编译器告诉我答案是6.
我想知道为什么答案是6.
谢谢提前。
-
问候。
Mockey Chen
电邮: mo ********* @ gmail.com
My friend ask me a question as following:
give a union SU define as:
typedef union _SU
{
short x;
struct y{
char a;
short b;
char c;
};
}SU;
what is the sizeof (SU)?
My compiler tell me the answer is 6.
I want to know why the answer is 6.
Thanks in advance.
--
Regards.
Mockey Chen
Email: mo*********@gmail.com
推荐答案
Mockey Chen< mo ********* @ google.com>写道:
Mockey Chen <mo*********@google.com> wrote:
typedef union _SU
{x>
struct y {
char a;
short b;
char c;
};
} SU;
sizeof(SU)是什么?
我的编译器告诉我答案是6. br />我想知道为什么答案是6。
typedef union _SU
{
short x;
struct y{
char a;
short b;
char c;
};
}SU;
what is the sizeof (SU)?
My compiler tell me the answer is 6.
I want to know why the answer is 6.
简答:为什么不呢?
更长的答案:
也许短裤在你的机器上宽4个字节...或者也许是
那里有对齐......或者...... ...... br />
Short answer: Why not?
Somewhat longer answer:
Maybe shorts are 4 bytes wide on your machine... Or maybe
there''s alignment... Or... ... ...
Sven Hesse写道:
Sven Hesse wrote:
Mockey Chen< mo ********* @ google .COM>写道:
Mockey Chen <mo*********@google.com> wrote:
typedef union _SU
{x>
struct y {
char a;
short b;
char c;
};
} SU;
sizeof(SU)是什么?
我的编译器告诉我答案是6. br />我想知道为什么答案是6。
typedef union _SU
{
short x;
struct y{
char a;
short b;
char c;
};
}SU;
what is the sizeof (SU)?
My compiler tell me the answer is 6.
I want to know why the answer is 6.
简答:为什么不呢?
更长的答案:
也许短裤是在您的机器上宽4个字节...或者也许
有对齐......或者...... ......
Short answer: Why not?
Somewhat longer answer:
Maybe shorts are 4 bytes wide on your machine... Or maybe
there''s alignment... Or... ... ...
结构struct y的大小是6个字节。短的大小是2
bytes。结构中有3个变量。每个字符也有两个大小为
的大小为2字节。
因此3 * 2字节= 6字节
The size of the structure struct y is 6 bytes.The size of short is 2
bytes.There are 3 variables in the structure.each character also has a
size of two bytes.
hence 3*2 bytes= 6 bytes
robotnik< fa ********* @ gmail.com>写道:
robotnik <fa*********@gmail.com> wrote:
每个字符也有两个字节的大小。
each character also has a size of two bytes.
....错误,因为什么时候是2字节宽的字符?如果我没有完全弄错的话,
char的_always_大小为1字节......
....Err, since when is a char 2 bytes wide? If I''m not totally mistaken,
a char has _always_ a size of 1 byte...
这篇关于联盟的规模的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!