I have a C++ class which has four private floats and a bunch of nonstatic public functions that operate on this data.
Is it guaranteed, or possible to make it so, that the four floats are contiguous and there is no padding. This would make the class the size of four floats, and it’s address would be that of the first float.
That depends on your compiler.
You can use
#pragma pack(1)with e.g. MSVC and gcc, or#pragma pack 1with aCC.For example, assuming
MSVC/gcc:Or better:
That basically disables padding and guarantees that the
floatsare contiguous. However, to ensure that the size of your class is actually4 * sizeof(float), it must not have a vtbl, which means virtual members are off-limits.