在C结构松弛字节 [英] Slack byte in C structures

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

问题描述

我怎能用字边界电脑卖场结构,知道单词边界的概念。我的工作在64位CPU,64位操作系统上。 limits.h中__WORDSIZE宏的值是64。因此,我的字的边界是8个字节吧?

I know the concept of word boundary whereby computer stores structures using word boundary. I am working on a 64 bit CPU with a 64 bit OS. The value of __WORDSIZE macro in limits.h is 64. So my word boundary is 8 bytes right?

我有两种结构:

struct a
{
int a;
char b;
float c;
};

的sizeof(结构A)给出了12的答案。的sizeof(INT)为4的sizeof(浮动)为4。

sizeof(struct a) gives 12 as answer. sizeof(int) is 4. sizeof(float) is 4.

For struct b{
char a;
char b;
char c;
char d;
};

的sizeof(结构b)为4。

sizeof(struct b) is 4.

这些输出暗示字边界为4个字节。如何找到这个词的边界。难道真的等于sizeof(int)的呢?

These outputs suggest the word boundary is 4 bytes. How do I find the word boundary. Is it really equal to sizeof(int) then?

奇怪的是:

struct c{
char a;
char b;
char c;
char d;
char e;
}

的sizeof(结构c)为5个字节。谁能解释这吧。

sizeof(struct c) is 5 bytes. Can anyone explain this please.

推荐答案

首先, __ WORDSIZE 是不是有你检查。除非他们明确你不应该使用带有开始宏(或其他名称)__ _ 和一个大写字母文件供您使用。它是通过在头使用的实现的内部部分,而实现可以自由删除,或者在以后的版本重新命名。其他系统可能甚至没有在所有的相应的宏,或者可以通过不同的名称有它

First of all, __WORDSIZE is not there for you to inspect. You should never use macros (or other names) beginning with __ or _ and a capital letter unless they're specifically documented for your use. It's an internal part of the implementation used by headers, and the implementation is free to remove or rename it in a later version. Other systems may not even have a corresponding macro at all, or may have it by a different name.

随着中说,结构调整无关的原生字的大小。它具有与结构的各个元件的对齐做

With that said, structure alignment has nothing to do with the native "word" size. It has to do with the alignments of the individual elements of the structure.

请参阅我的回答这个问题:

See my answer to this question:

<一个href=\"http://stackoverflow.com/questions/6963998/how-does-gcc-calculate-the-required-space-for-a-structure/6964021#6964021\">How海湾合作委员会不​​计算所需的空间结构?

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

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