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?

Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
Ben
  • 15,938
  • 19
  • 92
  • 138
  • 14
    Your analysis is correct. The question is ill-posed. – ruakh Mar 04 '12 at 23:52
  • Since understanding is more important than a circle on the paper, explain the correct answer in writing. – tenfour Mar 04 '12 at 23:53
  • The thing is, I emailed the "admissions-person" about it, who then contacted the course-leader. Apparently, said course-leader told them that there is only one true answer to this question. – Ben Mar 04 '12 at 23:56
  • 15
    @Ben, then the course leader is wrong. Maybe you should get your MSc somewhere where they know how C++ works? – Carl Norum Mar 04 '12 at 23:57
  • @Carl That's what I am thinking... I just wanted to make sure I am not misunderstanding the question or anything;) – Ben Mar 05 '12 at 00:00
  • @Ben: Theoretically, you could stuff a character with multiple boolean values. Practically, you would need to know what implementation is to be assumed but in most cases that would result in two answer being the right choice. So if he were to insist and he would require an answer I would have given `bool` because it is correct in both occasions. He doesn't ask *less than the others*... ;) – Tamara Wijsman Mar 05 '12 at 00:09
  • 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. – ruakh Mar 05 '12 at 00: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. – Ben Mar 05 '12 at 00: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. – ruakh Mar 05 '12 at 00:27
  • @ruakh What I meant to say is, if we start guessing what the author of the question might have intended to ask, we might well be able to come up with a number of possible scenarios... ;) – Ben Mar 05 '12 at 00:34
  • @Ben: I say, [Pascal's Wager](http://en.wikipedia.org/wiki/Pascal%27s_Wager) it. If the course leader thinks that a `bool` variable can be stored in one bit *in C++*, then (per Carl Norum) you don't want to go there, so it's O.K. to get the question wrong, and it doesn't matter what you put. If the course leader thinks that *least* means *guaranteed least possible* rather than *less than any other*, then that's not a big deal, so you'd rather get the question right, and you should put `char`. Given one theory that doesn't care, and one that does, it's safer to go with the latter. – ruakh Mar 05 '12 at 00:40
  • @ruakh: I've seen compilers which really use only one bit, but the programmer has to mark it specifically as `bit` instead of `char` or `bool`. You should only use it if you know what you are doing, because they take place not in RAM, but in GPIO registers. – vsz Mar 05 '12 at 07:47

10 Answers10

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

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • Okay, I pull back my words. The documentation on the return value probably explains that... – Tamara Wijsman Mar 05 '12 at 00:06
  • 1
    @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? – Mooing Duck Mar 05 '12 at 06:29
  • @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-qa mort-ora-y Mar 05 '12 at 07:19
  • @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. – Mooing Duck Mar 05 '12 at 15:24
  • 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. – 12431234123412341234123 Jul 13 '21 at 11:30
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...

Tamara Wijsman
  • 12,198
  • 8
  • 53
  • 82
  • 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. – edA-qa mort-ora-y Mar 05 '12 at 09:11
  • 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. – edA-qa mort-ora-y Mar 05 '12 at 09:47
  • 5.3.3.-1: _"sizeof(char), sizeof(signed char) and sizeof(unsigned char) are 1"_ – edA-qa mort-ora-y Mar 05 '12 at 10:09
  • How could something be less than 1 in size? Also, for Unicode they have char16_t/char32_t now, or char for UTF-8 (which must be big enough for UTF8) – edA-qa mort-ora-y Mar 05 '12 at 10:13
  • The basic code set, as well as minimum size of a byte, and basic UTf-8 requirements are specified in the standard itself. That can never change anymore. – edA-qa mort-ora-y Mar 05 '12 at 10:14
  • 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-qa mort-ora-y Mar 05 '12 at 10:16
  • 1
    @edA-qamort-ora-y: As per [size_t](http://www.cplusplus.com/reference/clibrary/cstring/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... :) – Tamara Wijsman Mar 05 '12 at 10:27
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++)?"

Community
  • 1
  • 1
Ben Lee
  • 52,489
  • 13
  • 125
  • 145
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.

Carl Norum
  • 219,201
  • 40
  • 422
  • 469
  • 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. – vsz Mar 05 '12 at 05:41
  • 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. – Carl Norum Mar 05 '12 at 05:49
  • 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? – vsz Mar 05 '12 at 06:08
  • 4
    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. – Carl Norum Mar 05 '12 at 06:09
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++?

Community
  • 1
  • 1
Roland Bouman
  • 31,125
  • 6
  • 66
  • 67
2

The C++ standard gives following relations:

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

...

Thomas Maierhofer
  • 2,665
  • 18
  • 33
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.

vsz
  • 4,811
  • 7
  • 41
  • 78
0

sizeof(bool) is implementation-defined.

Is sizeof(bool) defined?

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

Community
  • 1
  • 1
Joe
  • 41,484
  • 20
  • 104
  • 125
  • 2
    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. – Carl Norum Mar 04 '12 at 23:56
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.

Ryan
  • 2,755
  • 16
  • 30
  • 1
    Not according to the language spec, you can't. A single `bool` would still have size `1` at minimum. – Carl Norum Mar 04 '12 at 23:58
  • @CarlNorum: Though IIRC, `std::vector` is specialized as a bit-string, one bit per element. (Or is that why you specified "a single `bool`"?) – ruakh Mar 05 '12 at 00:00
  • 5
    It's a question about a particular language, not a theoretical language. – Johan Lundberg Mar 05 '12 at 00:01
  • @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". – Ben Mar 05 '12 at 00:04
  • some compilers designed for mictrocontrollers have a bit variable, which really only takes 1 bit: so 8 of them needs only 1 byte. – vsz Mar 05 '12 at 07:38
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.

Dmitriy Kachko
  • 2,804
  • 1
  • 19
  • 21