Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 636 Vote(s) - 3.53 Average
  • 1
  • 2
  • 3
  • 4
  • 5
C vs C++ struct alignment

#1
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][1] struct to a reference to C++ wrapper around this struct with some helper methods. Can you please clarify this matter?

[1]:

[To see links please register here]

Reply

#2
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.
Reply

#3
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][1] 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.

[1]:

[To see links please register here]

Reply

#4
When C++ was developed, the developers figured out that C programmers relied on some things that the C++ developers didn't want to guarantee, but not guaranteeing them would mean that a lot of C code that was also valid C++ code would be broken when used as C++ code. Not desirable.

That's why they invented "POD" structures: A struct that didn't use any C++ features would behave in a C++ program exactly as it would in a C program (apart from the fact that implementation defined behaviour could change, since a C compiler and a C++ compiler are clearly not the same implementation. On the other hand, the C++ compiler would probably just copy the implementation definition from the C compiler).

If you take any plain C struct that is also a valid C++ struct (no members named "class", for example), and then you just add "public:" just after the opening brace, then its layout, order of members, alignment and so on can all change. Even though all struct members are by default public, so nothing really changed. Except that because of the "public:" it's not a POD anymore.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through