为什么布尔消耗不是char更多的内存? [英] Why does boolean consume more memory than char?
本文介绍了为什么布尔消耗不是char更多的内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为什么一个布尔占用4个字节,一个char 2个字节在.NET框架?布尔要占用1位,或至少会比字符小。
Why does a Boolean consume 4 bytes and a char 2 bytes in the .NET framework? A Boolean should take up 1bit or at least be smaller than a char.
推荐答案
这是内存对齐的问题。 4字节变量工作比2字节的更快。这就是为什么你应该使用int,而不是字节或短的计数器之类的原因。
It is a question of memory alignment. 4-byte variables work faster than 2-byte ones. This is the reason why you should use int instead of byte or short for counters and the like.
您应该使用2个字节的变量,只有当内存比速度的一个更大的问题。这就是为什么CHAR(这是统一code。在.NET)采用了四两字节,而不是原因。
You should use 2-byte variables only when memory is a bigger concern than speed. And this is the reason why char (which is Unicode in .NET) takes two bytes instead of four.
这篇关于为什么布尔消耗不是char更多的内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文