Memory allocation within structs

I am getting conflicting results when I ask root and my C++ compiler (gcc 3.2.3) to evaluate the memory size of structs.

For example, the following trivial macro asks the C++ compiler to return the memory size of a “float” and of a simple struct called “Struct”:

#include <iostream>

struct Struct{
    float one,two,three;

int main(){
    cout<<"sizeof(float) = "<<sizeof(float)<<endl;
    cout<<"sizeof(event)   = "<<sizeof(Struct)  <<endl;

When I run this macro using gcc, I get the following output:

sizeof(float)     = 4
sizeof(event)   = 12

However, when I run this same basic macro in root, I get:

sizeof(float)     = 4
sizeof(event)   = 16

Why does root seemingly add an extra 4 bytes onto the size of the same struct given that the struct is made up of only floats which both root and gcc agree upon the size of? Is there a way to make them agree? I am aware that root has its own system-independent variable classes (e.g. Int_t, Float_t, etc.). However, changing the code to utilize these classes still gives the same results.

Thank you,



That is expected; there is no definition of the memory layout of classes / structs in the C++ standard.

The issue is e.g. padding due to alignment, packing, virtual tables, etc. There is no way that Cint can do the same as all compilers, because all compilers do it differently, see e.g. on how MS Visual C does it (which shows you just how many options compilers have).

The solution is: compile your code (.L MyCode.C+) - that way the memory layout is defined by your compiler, not by Cint.