工会规模 [英] union size
问题描述
#include< stdio.h>
// #include< strlen.h>
union xxx
{
char name [10];
double d;
int ff;
};
int main()
{
union xxx s;
printf("%d \ n", sizeof(s));
getch();
返回0;
}
为什么o / p在GCC编译器中是16?
#include <stdio.h>
//#include <strlen.h>
union xxx
{
char name[10];
double d;
int ff;
};
int main()
{
union xxx s;
printf("%d\n",sizeof(s));
getch();
return 0;
}
why o/p is 16 in GCC compiler ??
推荐答案
asit写道:
asit wrote:
#include< stdio.h>
union xxx
{
char name [10];
双d;
int ff;
};
int main()
{
union xxx s;
printf("%d \ n",sizeof(s));
getch();
返回0;
}
为什么在GCC编译器中o / p为16?
#include <stdio.h>
union xxx
{
char name[10];
double d;
int ff;
};
int main()
{
union xxx s;
printf("%d\n",sizeof(s));
getch();
return 0;
}
why o/p is 16 in GCC compiler ??
这个东西被称为''padding'',由于你的平台上有''对齐约束''
。这两个术语和一个网络搜索应该会产生很多
的解释。 ;)
Uli
The thing is called ''padding'' and necessary due to ''alignment constraints''
on your platform. Those two terms and a websearch should yield lots of
explanations. ;)
Uli
asit写道:
....
asit wrote:
....
printf("%d \ n",sizeof(s));
printf("%d\n",sizeof(s));
%d格式代码要求相应的参数具有
提升类型的''int''。 sizeof()返回一个size_t类型的值,
可能是也可能不是unsigned int,但保证是无符号类型
某种类型。它很有可能成为比''int'更大的类型。
在C89中,你应该使用
printf( "%lu \ n",(unsigned long)sizeof(s));
在C99中,它更简单:
printf("%zu \ n",sizeof(s));
The "%d" format code requires that the corresponding argument have a
promoted type of ''int''. sizeof() returns a value of type size_t, which
may or may not be unsigned int, but is guaranteed to be an unsigned type
of some kind. It has a pretty good chance of being a type larger than ''int''.
In C89, you should use
printf("%lu\n", (unsigned long)sizeof(s));
In C99, it''s simpler:
printf("%zu\n", sizeof(s));
asit写道:
asit wrote:
#include< stdio.h>
// #include< strlen.h>
union xxx
{
char name [10];
double d;
int ff;
};
int main()
{
union xxx s;
printf("%d \ n", sizeof(s));
getch();
返回0;
}
为什么在GCC编译器中o / p是16?
#include <stdio.h>
//#include <strlen.h>
union xxx
{
char name[10];
double d;
int ff;
};
int main()
{
union xxx s;
printf("%d\n",sizeof(s));
getch();
return 0;
}
why o/p is 16 in GCC compiler ??
假设目前通过
`int`-打印格式打印`size_t`并不会让你进入替换
现实[1],这是因为`union xxx`对象的大小为16.
它的大小必须达到至少10,因为'name`
字段。如果您的实现需要
正确地对齐这些类型以提高效率,它必须是`double`和`int`的大小的整数倍。让我们假设
我们现在可以忽略`int`而支持`double`。
如果double'的大小为2 ,5或10,不需要额外的填充来获得正确的对齐。如果它是3或4
或6,我们只需要填充到12个字节;如果是7,
我们只需要填充到14个字节。
8但是,需要填充到16个字节。我希望你的实现上
sizeof double = 16,并且那是你的
看到填充要求。
[ 1]注意Cybermen,earpods和9位字节。
-
Hedgehog医生
" Life充满了神秘感。考虑其中一个。 Sinclair,/ Babylon 5 /
Assuming for the moment that printing a `size_t` through an
`int`-printing format doesn''t shift you into an alternate
reality [1], it''s because a `union xxx` object has size 16.
It''s got to have size at least 10, because of the `name`
field. And it''s got to be an integer multiple of the size
of `double` and `int` if your implementation needs to
align those types properly for efficiency. Let''s suppose
we can ignore the `int` in favour of the `double` for now.
If double''s size were 2, 5, or 10 there''d not need to be
extra padding to get the alignment right. If it were 3 or 4
or 6, we''d only need to pad to 12 bytes; if it were 7,
we''d only need to pad to 14 bytes.
8, however, requires padding to 16 bytes. I expect that
sizeof double = 16 on your implementation and that''s your
seeing the padding requirement.
[1] Watch out for Cybermen, earpods, and nine-bit bytes.
--
Doctor Hedgehog
"Life is full of mysteries. Consider this one of them." Sinclair, /Babylon 5/
这篇关于工会规模的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!