The padding causes the IR structure to be inconsistent with the gdb output

When I compile my project for IR, I get the following structure definition:

%class.Location = type { i8, i32, i32, i32 }

I then compiled my project as an executable and debugged it using gdb (I didn’t generate the executable from IR, I recompiled it once).

I used gdb to output the definition of the structure/class and it returned me the following result:

/*  26074      |      16 */            class Location {
                                         public:
/*  26074: 0   |       1 */                uint8_t relative_alt : 1;
/*  26074: 1   |       1 */                uint8_t loiter_ccw : 1;
/*  26074: 2   |       1 */                uint8_t terrain_alt : 1;
/*  26074: 3   |       1 */                uint8_t origin_alt : 1;
/*  26074: 4   |       1 */                uint8_t loiter_xtrack : 1;
/* XXX  3-bit hole       */
/* XXX  3-byte hole      */
/*  26078      |       4 */                int32_t alt;
/*  26082      |       4 */                int32_t lat;
/*  26086      |       4 */                int32_t lng;
                                         private:
                                           static const float LOCATION_SCALING_FACTOR;
                                           static const float LOCATION_SCALING_FACTOR_INV;

                                           /* total size (bytes):   16 */
                                       } _target_location;

Obviously the two structures are not the same size, anyone tell me why?

Why is it obvious they are not the same size? Both seem to say “16” to me.

Isn’t the size of %class.Location i8+i32+i32+i32 = 13 bytes?

There are 3 bytes of padding here to keep the int32_t types alighed properly.

1 Like

Isn’t the size of |%class.Location| |i8+i32+i32+i32 = 13| bytes?

No. Struct types in LLVM will naturally align inner types depending on
the data layout. You need to use the DataLayout class in LLVM to
accurately compute the size of a struct, but i32 is usually aligned to
4 bytes, which means there will be three bytes of padding following the
i8 to make the struct size 16 bytes over all.

Note that there are packed struct types which do not include any
aligning of fields internally, but the struct you have posted here is
not using a packed struct type.

1 Like