C vs C++ struct alignment

Both the C and C++ language standards make no requirements of struct padding and leave it to be a compiler implementation detail. A strict interpretation of this would mean that there is no guarantee a struct would be the same between the two.

In practice, however, a given version of a toolchain capable of both C and C++ (such as GCC or Clang) can pack an identical struct in the same manner, if needed. Without this, a lot of production code in the world simply wouldn’t work. This is a guarantee given by the toolchain, however, and not the language.

It is worth noting that if you were to declare a similar struct to the C original, but added access specifiers (private, public and protected), that the layout would change, but that’s a bit of a stretch since the struct is no longer identical.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)