了解32位C编译器的sizeof(字符) [英] Understanding sizeof(char) in 32 bit C compilers

查看:563
本文介绍了了解32位C编译器的sizeof(字符)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(sizeof的)字符总是返回 1 在32位编译器。

(sizeof) char always returns 1 in 32 bit GCC compiler.

但是,因为在32位的编译器的基本块大小为4,怎样炭占据一个字节时的基本大小为4字节???

But since the basic block size in 32 bit compiler is 4, How does char occupy a single byte when the basic size is 4 bytes???

考虑以下内容:

struct st 
{
int a;
char c;
};

的sizeof(ST)返回8 同为4个字节的(因为2块被分配)

我永远无法理解为什么的sizeof(char)的返回为1 当分配大小为4块。

I can never understand why sizeof(char) returns as 1 when it is allotted a block of size 4.

有人能请解释一下?

我将是解释它的任何答复非常感谢!

I would be very thankful for any replies explaining it!!!

编辑:'位'的错字已更改为字节。我问对不起,谁做的第一次编辑的人。我rollbacked的编辑,因为我没有注意到ü所做的更改。
感谢所有那些谁做的一个点,它必须改变,尤其是@Mike伯顿为downvoting的问题和@jalf谁似乎在我的概念的理解妄下结论!

EDIT : The typo of 'bits' has been changed to 'bytes'. I ask Sorry to the person who made the first edit. I rollbacked the EDIT since I did not notice the change U made. Thanks to all those who made it a point that It must be changed especially @Mike Burton for downvoting the question and to @jalf who seemed to jump to conclusions over my understanding of concepts!!

推荐答案

的sizeof(char)的始终为1。永远。在块大小你所谈论的仅仅是机器的本地字大小 - 通常这将导致在最高效的操作规模。您的计算机仍然可以分别应对字节 - 这是什么的sizeof 运算符告诉你有关。当你做的sizeof(INT),它返回4地告诉你,一个 INT 为您的机器上4个字节。同样的,你的结构是8个字节长。有一个从有多少位有一个字节中没有信息的sizeof

sizeof(char) is always 1. Always. The 'block size' you're talking about is just the native word size of the machine - usually the size that will result in most efficient operation. Your computer can still address each byte individually - that's what the sizeof operator is telling you about. When you do sizeof(int), it returns 4 to tell you that an int is 4 bytes on your machine. Likewise, your structure is 8 bytes long. There is no information from sizeof about how many bits there are in a byte.

的原因,您的结构是8个字节长,而不是5(正如您所料),是编译器添加的填充应用于结构,以把一切都很好地对准到本地字长再次,以提高效率。大多数编译器给你选项的的结构,无论是与一个的#pragma 指令或某些其他编译器扩展,在这种情况下,你可以强迫你结构采取最小尺寸,不管你的机器的字长。

The reason your structure is 8 bytes long rather than 5 (as you might expect), is that the compiler is adding padding to the structure in order to keep everything nicely aligned to that native word length, again for greater efficiency. Most compilers give you the option to pack a structure, either with a #pragma directive or some other compiler extension, in which case you can force your structure to take minimum size, regardless of your machine's word length.

字符的大小为1,因为这是最小的访问权限大小您的计算机可以处理 - 对于大多数机​​器是一个8位值。在的sizeof 运营商为您提供所有其他量的多少字符目标为单位的大小将是大小相同无论你问。在填充的(见下面的链接),由编译器添加到性能方面的原因你的数据结构,所以在实践中大于你可能会觉得从刚才看的结构定义。

char is size 1, since that's the smallest access size your computer can handle - for most machines an 8-bit value. The sizeof operator gives you the size of all other quantities in units of how many char objects would be the same size as whatever you asked about. The padding (see link below) is added by the compiler to your data structure for performance reasons, so it is larger in practice than you might think from just looking at the structure definition.

有一个名为数据结构调整维基百科的文章有一个很好的解释和例子。

There is a wikipedia article called Data structure alignment which has a good explanation and examples.

这篇关于了解32位C编译器的sizeof(字符)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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