C vs C ++ struct alignment

In a recent interview, I was told about alignment of C ++ structural fields and theorized that C and C ++ follow the same strategy in structured packaging.

Hover, that was a wrong assumption. The interviewer said that in general, C and C ++ pack structures differently, and we should never expect the opposite. IMHO this is a strange expression. There is no pack "C" qualifier for structures in C ++ for use in bilingual C / C ++ header files.

Thus, in practice, this may mean that you cannot create a structure in C ++ and pass it to the C library, because in general its fields will be aligned in a different way and will have different offsets. But in fact, most programmers seriously rely on this interoperability until they convert a pointer to a C POD structure into a reference to a C ++ wrapper around this structure using some helper methods. Could you clarify this issue?

+61
c ++ c pointers struct
Nov 20 '15 at 20:31
source share
3 answers

Both C and C ++ languages ​​do not require requirements for filling in the structure and leave this with details of the compiler implementation. A strict interpretation of this means that there is no guarantee that the structure will be the same between the two.

In practice, however, this version of the toolchain, capable of both C and C ++ (such as GCC or Clang), can pack the same structure if necessary. Without this, a lot of production code in the world simply will not work. However, this is a guarantee provided by the toolchain, not the language.

It is worth noting that if you declared a similar structure to be the original C, but added access specifiers ( private , public and protected ), then the layout has changed, but a little stretch, since the structure is no longer identical.

+67
Nov 20 '15 at 20:46
source share

This was most obviously wrong (on the interviewer's side). It is clear that the packaging structure is the same for C and C ++ for those who have worked with any low-level API regarding structures - for example, the network API. These are all C functions that accept C structures, but they are safely called millions of millions of times a day from C ++ code.

You should be fortunate that you had this question. This makes it clear that you should not work there.

+28
Nov 20 '15 at 20:44
source share

When C ++ was developed, the developers found out that C programmers rely on some things that C ++ developers did not want to guarantee, but without guaranteeing them, would mean that a lot of C code, which was also valid, C ++ code would break when used as C ++ code. Undesirable.

That's why they invented the "POD" structure: a structure that didn't use any C ++ functions could behave in a C ++ program exactly the same as in a C program (except that the behavior defined by the implementation can change , since the C compiler and the C ++ compiler are clearly not the same implementation. On the other hand, the C ++ compiler will probably just copy the implementation definition from the C compiler).

If you take some simple C-structure, which is also a valid C ++ structure (for example, not members with the name "class"), and then you just add "public:" right after opening the bracket, then its layout, order members, alignment, etc. everything can change. Although all members of the structure are public by default, nothing has changed. Except that due to the "public" it is no longer a POD.

+4
Nov 21 '15 at 19:38
source share



All Articles