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:
C++ Basics
If a program declared four variables, one of type
int
, one of typefloat
, one of typechar
, and one of typebool
, which variable would occupy the least space in memory?
- int
- char
- float
- bool
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?
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."
Floats on the other hand lose value range in favor of using some bits for decimal range. "Floats [...] lose value range" makes it sound as if the largest number representable by a floating point number was smaller than the largest number representable by an integer number of the same size, which is definitely not true.
In practice, char is usually 8 bits in size and short is usually 16 bits in size (as are their unsigned counterparts).
We omit for the moment the bool and char data types in c# which could be qualified as integers but which should be treated separately. A byte type variable is coded on one byte (8 bits). It can take any integer value included between 0 and 255 (inclusive).
The correct answer is boolean in theory, as a char requires knowledge of at least 8 bits, while a bool technically only requires one bit. you could smash 8 bools inside of a single char if you wanted to in theory. Not according to the language spec, you can't. A single bool would still have size 1 at minimum.
Like bool, byte, int, long, float, double, and decimal, a char type is of type value: 16 bits (and nothing else) are reserved on the stack for a variable of type char.
Assigning of an int value to a variable of float type results in the fractional part becoming zero. Precision is usually lost if the integer has more bits than the floating variable can accommodate. If we try to assign an out of range value to a variable of unsigned type, the result is the remainder of the value % (modulo)
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.
(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.)
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?
The real problem with the question your have posted lies in these words:
occupy ... space in memory
If an interpretation is to be assumed, then in most occasions you would assume one of the current popular compilers in which case answer 2
and 4
would both occupy the least space in memory. Simply because the current popular compilers make the char
and bool
occupy a single byte in memory...
As outlined in the comments, sizeof() is of type size_t
, which is integral.
As sizeof(char) == 1
is always true as per the standard, and the value is integral; no other sizeof(T)
can be lower than 1
. But any other T
than char
can be bigger than 1
dependening on the implementation. As you can't assume that sizeof(char) == sizeof(bool)
always holds, you can at least assume that sizeof(char) <= sizeof(bool)
holds.
Which makes sizeof(char)
being the least the most correct answer...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With