Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C vs C++ struct alignment

I've been asked in a recent interview about C++ struct fields alignment and theoretized that C and C++ follows the same strategy in struct packing.

Hovewer, it was the wrong assumption. The interviewer said that in general C and C++ are packing structs in different ways and we should never expect the opposite. IMHO it's strange statement. There is no pack "C" qualifier for structs in C++ for using in bilingual C/C++ header files.

So in practice it could mean that you can't create a struct in C++ and pass it to a C library because in general its fields will be aligned in a different way and have different offsets. But, in fact, most programmers seriously rely on this interoperability up to the point when they convert a pointer to a C POD struct to a reference to C++ wrapper around this struct with some helper methods. Can you please clarify this matter?

like image 595
Minor Threat Avatar asked Nov 20 '15 20:11

Minor Threat


People also ask

Are structs 8 byte aligned?

Assuming a 64-bit machine, any instance of struct foo1 will have 8-byte alignment.

What is alignment in C programming?

One of the low-level features of C is the ability to specify the precise alignment of objects in memory to take maximum advantage of the hardware architecture. CPUs read and write memory more efficiently when they store data at an address that's a multiple of the data size.

What is structure member alignment?

Data structure alignment is the way data is arranged and accessed in computer memory. Data alignment and Data structure padding are two different issues but are related to each other and together known as Data Structure alignment.


2 Answers

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.

like image 73
Sam Cristall Avatar answered Sep 22 '22 04:09

Sam Cristall


This was most obviously wrong (on the interviewer side). It is clear to see that struct packing is the same for C and C++ for anybody who worked with any low-level API dealing with structs - for instance, a network API. All those are C functions, which accept 'C' structs, yet they are safely called millions of millions times a single day from C++ code.

You should be lucky you had this question. It makes it clear you should not be working there.

like image 23
SergeyA Avatar answered Sep 21 '22 04:09

SergeyA