Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Of int, char, float, and bool, which is smallest?

Tags:

c++

types

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 type float, one of type char, and one of type bool, which variable would occupy the least space in memory?

  1. int
  2. char
  3. float
  4. 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?

like image 517
Ben Avatar asked Mar 04 '12 23:03

Ben


People also ask

Is bool smaller than char?

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."

Which is bigger in size int or float?

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.

Is Char smaller than short?

In practice, char is usually 8 bits in size and short is usually 16 bits in size (as are their unsigned counterparts).

Are Bool and char data types integers?

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).

What is the difference between a char and a bool?

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.

What is a char type in C++?

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.

What happens when you assign an int value to a float?

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)


2 Answers

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.)

like image 183
Kerrek SB Avatar answered Oct 23 '22 11:10

Kerrek SB


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...

like image 12
Tamara Wijsman Avatar answered Oct 23 '22 11:10

Tamara Wijsman