30

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?

12
  • 14
    Your analysis is correct. The question is ill-posed. Commented Mar 4, 2012 at 23:52
  • 15
    @Ben, then the course leader is wrong. Maybe you should get your MSc somewhere where they know how C++ works? Commented Mar 4, 2012 at 23:57
  • 4
    You know, if the course leader is not a native English speaker, there could be a language barrier here. I remember I once had an exam question of the form "Verify that ____". The correct answer, as it turned out, was that ____ was false: the professor simply misunderstood the meaning of "verify that", and took it to mean something like "determine whether". In your case, the course leader might intend char as the correct answer because a char variable takes the least possible amount of space, not realizing that that's not what the question sounds like to native speakers. Commented Mar 5, 2012 at 0:15
  • 1
    @ruakh Or, he might intend bool as the correct answer, because in theory, a boolean value (true/false) could be stored in one bit. Commented Mar 5, 2012 at 0:23
  • 1
    @Ben: Yes, that would be the theory that the course leader has very basic misunderstandings about C++; others have already covered that. I'm offering an alternative theory: that the course leader has not fully grasped the pragmatics of English superlatives. Commented Mar 5, 2012 at 0:27

10 Answers 10

36

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

Sign up to request clarification or add additional context in comments.

5 Comments

Okay, I pull back my words. The documentation on the return value probably explains that...
@KerrekSB: I saw that debated recently, and was unable to find anything in the spec that says "fundamental machine/memory addressable unit"; instead the spec said "each byte must have a unique address", which would allow 8 bit "emulation" where 16 bits is the minimal (hardware) addressable unit. Can you find me a bit in the spec to back that?
@MooingDuck, 1.7 "C++ memory model", paragraph 1. Bytes must be contiguous, must be at least 8-bits. "Machine" is actually the abstract machine though, the underlying hardware can be different.
@edA-qamoert-ora-y that says nothing about a byte being the minimal addressable unit. The differentiation between "machine" and "hardware" either implies I'm right or makes this moot, I'm not sure which.
I don't think the last part is correct, i don't think it is possible to fulfill the float specifications with only 16 bit, you already need at least 19.9 bit (or 20 bit) to represent the required 6 decimal digits of the mantissa, AFAIK you need at least 32 bit. But there is nothing that stops a C++-implementation using 32 bit for all 4 mentioned types.
12

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

7 Comments

If he insists you should take char. As Kerrek SB indicates in his answer, it is the only one guaranteed to have a size of 1. A bool can be larger than that.
No, it can't. It's by definition in the standard. sizeof(char) == 1. Since size is always integral a bool cannot be smaller than 1.
5.3.3.-1: "sizeof(char), sizeof(signed char) and sizeof(unsigned char) are 1"
You mean that it is "integral"? 5.3.3-6. "The result of sizeof and sizeof... is a constant of type std::size_t. "
@edA-qamort-ora-y: As per size_t I assume you are correct and have amended my answer accordingly, I don't have access to the standard but see multiple users mention it and will verify in the near future. Thanks for clarifying this... :)
|
5

The answer is char. No other answer is correct.

(Though I agree the question should have been worded better).

The C++03 Standard $5.3.3/1 says:

sizeof(char), sizeof(signed char) and sizeof(unsigned char) are 1; the result of sizeof applied to any other fundamental type (3.9.1) is implementation-defined. [Note: in particular, sizeof(bool) and sizeof(wchar_t) are implementation-defined.69)

(Found this info from another question: Why the sizeof(bool) is not defined to be one, by the Standard itself?).

Given that the minimum size is 1 (sizeof must return integral values), this means that the following will be true in any implementation that follows the standards:

sizeof(char) == 1
sizeof(bool) >= 1
sizeof(int) >= 1
sizeof(float) >= 1

The question was poorly phrased and probably should have been asked more clearly as "...which variable would necessarily occupy no more space in memory than any other (in any well-behaved standard implementation of C++)?"

Comments

2

The language doesn't specify any relationships between these type sizes that guarantee a correct answer to that question as posed. They could all be 32-bit types, for example.

4 Comments

It does. While the exact size is not specified, it is guaranteed, that char will not be bigger than the others. So an answer does exist for this question.
Yeah - so they might all be the same size, meaning they would all take the same amount of space in memeory. The question doesn't specify the environment, so it's not answerable. The char will certainly always be least regardless of environment, but the others might all be the same.
What is the smallest from this list> 5, 2, 3, 2, 2, 4 ? Can you say there is no answer, or maybe it's better to say that 2 is the smallest, even if it's not alone to be the smallest?
That's not the same question. More analogous would be "which index from that list contains the lowest value"? There are three equally good answers to that question.
2

I think the correct answer should be 2. By definition, char is the smallest addressable unit.

Also see: Why is a char and a bool the same size in c++?

Comments

2

The C++ standard gives following relations:

sizeof(char) == 1
sizeof(char) <= sizeof(int) <= sizeof(long)
sizeof(float) <= sizeof(double)

...

1 Comment

What section of the standard or Stroustrup mentions that? This however does not cover sizeof(bool)...
1

There is no guarantee for the exact size of these types, but there is a guarantee, that char is not bigger than short, and short is not bigger than long.

So, char will always occupy the least amount of memory, but it might not be the only one to do so. It's still guaranteed, that nothing else will have a smaller size.

There might be an exception with bool, however, on some special embedded microcontrollers. They can have a bit variable, which takes exactly one bit, however, they are not in RAM but in special registers.

However, unless your architecture and compiler are especially strange or unusual, you can reasonalbly expect that char is 1, short is 2, long is 4, long long is 8 and int is either 2 or 4, but usually 4 bytes long.

Comments

0

sizeof(bool) is implementation-defined.

Is sizeof(bool) defined?

Namely, it is not required to only be a single byte.

1 Comment

The only one that's required to be a single byte is char. But all of the other ones could have size 1 as well.
0

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.

5 Comments

Not according to the language spec, you can't. A single bool would still have size 1 at minimum.
@CarlNorum: Though IIRC, std::vector<bool> is specialized as a bit-string, one bit per element. (Or is that why you specified "a single bool"?)
It's a question about a particular language, not a theoretical language.
@Ryan Yes, that is what I have been thinking, in theory you are right. And I think this is what they meant to ask. However, the test explicitly says "C++ Basics".
some compilers designed for mictrocontrollers have a bit variable, which really only takes 1 bit: so 8 of them needs only 1 byte.
0

The typical size of a bool is one byte. does not means it always is one byte. The question either refers to a realization that have not one-byte-sized bool or implies that only one variable has a smallest size.

3 Comments

Note that a bool cannot be smaller than a char/byte.
@MooingDuck: What section of the standard tells us that?
@TomWijsman The standard states that sizeof(char) == 1. Since sizeof(t) is integral, no type can be smaller.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.