问题描述
以下内容来自在家填写"编程测试,该测试是英国大学游戏开发理学硕士课程申请过程的一部分:
The following is from a "fill-in at home" programming test that is part of the application process for an MSc in game development at a UK university:
如果程序声明了四个变量,则类型为 int
,类型之一 float
,一种类型为 char
,另一种类型为 bool
,该变量会占用内存中的最小空间?
If a program declared four variables, one of type int
, one of type float
, one of type char
, and one of type bool
, which variable would occupy the least space in memory?
- int
- char
- 浮动
- 布尔
根据说明,只有一个正确的陈述.但是,我的C ++书(C ++ Pocket Reference,O'Reilly)指出:"bool的典型大小是一个字节,"和"char的大小是一个字节.从技术上讲,字节的大小是实现定义的,但是几乎只有八位."
According to the instructions, there is only one true statement. However, my C++ book (C++ Pocket Reference, O'Reilly) states: "The typical size of a bool is one byte," and "The size of a char is one byte. The size of a byte technically is implementation defined, but it is rarely anything but eight bits."
我在这里误会了什么吗?您会回答什么,为什么?
Am I misunderstanding something here? What answer would you put and why?
推荐答案
没有任何类型比 char
少 ,因为根据定义, sizeof(char)==1
.但是,所有类型完全有可能占用相同数量的空间.
No type takes less than char
, because by definition sizeof(char) == 1
. However, it is entirely possible that all types take the same amount of space.
(用16位(用适当的异常浮点格式表示每种类型就足够满足标准值范围的要求;每种类型都有32位的实际硬件就存在).
(Representing each type with 16 bits (with a suitably unusual floating point format) would suffice to satisfy the standard value range requirements; real hardware where every type has 32 bits exists.)
这篇关于在int,char,float和bool中,哪一个最小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!