In a typical C++ implementation, what is the result of sizeof applied to an empty class?
#include <iostream>
using namespace std;
class Empty
{
};
int main()
{
cout << sizeof(Empty);
return 0;
}
Options
-
A.
Always 0
-
B.
Typically 1 byte
-
C.
Always 4 bytes
-
D.
More than one of the above
-
E.
None of the above
Correct Answer (Detailed Explanation is Below)
B.
Typically 1 byte
Detailed Explanation
Correct Answer: Option BExplanation:A complete object of an empty class must have a nonzero size so that distinct objects can have distinct addresses. In typical C++ implementations,
sizeof(Empty) is 1 byte. The C++ standard does not require every implementation to use exactly one byte.