C++: What is the size of an object of an empty class?

后端 未结 16 1614
悲&欢浪女
悲&欢浪女 2020-11-22 11:54

I was wondering what could be the size of an object of an empty class. It surely could not be 0 bytes since it should be possible to reference and

相关标签:
16条回答
  • 2020-11-22 12:37

    I think it might be helpful to link to an answer explaining this good too. It is about boost::compressed_pair by Logan Capaldo.

    0 讨论(0)
  • 2020-11-22 12:39

    Quoting Bjarne Stroustrup's C++ Style and Technique FAQ, the reason the size is non-zero is "To ensure that the addresses of two different objects will be different." And the size can be 1 because alignment doesn't matter here, as there is nothing to actually look at.

    0 讨论(0)
  • 2020-11-22 12:40

    The standard states that all most derived objects have sizeof() >= 1:

    Unless it is a bit-field (class.bit), a most derived object shall have a non-zero size and shall occupy one or more bytes of storage. Base class sub-objects may have zero size. ISO/IEC FDIS 14882:1998(E) intro.object

    0 讨论(0)
  • 2020-11-22 12:41

    That's really an implementation detail. Once long ago, I thought it could be zero bytes or a thousand bytes, that it has no bearing on the language specification. But, after looking at the standard (section 5.3.3), sizeof is defined as always returning one or greater, no matter what.

    The size of a most derived class shall be greater than zero.

    This is required for, among other things, allowing you to handle arrays of objects and pointers to them. If your elements were allowed to be zero-sized then &(array[0]) would be identical to &(array[42]), which is going to cause all sorts of havoc to your processing loops.

    The reason why it may not be a machine word is that there are no elements within it that actually require it to be aligned on a word boundary (such as an integer). For example, if you place char x; int y; inside the class, my GCC clocks it at eight bytes (since the second int must be aligned in that implementation).

    0 讨论(0)
  • 2020-11-22 12:42

    Allocation of 1 byte for an empty class is compiler dependent. Compilers need to make sure objects reside in different memory locations and they need to allocate non zero memory size to an object. Listen to notes on this topic here: http://listenvoice.com/listenVoiceNote.aspx?id=27

    Even though compilers allocates non zero size to an empty class they also do optimizations when new classes are derived from empty classes. Listen about empty base optimization on ListenVoice's c++ programming interview questions.

    0 讨论(0)
  • 2020-11-22 12:43

    I think it is so because as 1 byte is the smallest memory unit that can be used as a placeholder, and it cannot give zero size as it will not be possible to create an array of objects ..

    and the thing you said "This was a little surprising to me since I was expecting it to be of the size of the machine word (32 bits or 4 bytes)." will be true for reference variable(macine words) of type empty(),not size of class itself(which is abstract data type),

    0 讨论(0)
提交回复
热议问题