1

The standard states that

Standard layout types are useful for communicating with code written in other programming languages.

But for interprocess communication on the same machine this is only true if the same member alignments are used, e.g. pramga pack(4)

Now lets add other machines to the mix and ignore endianess for a moment. Would pragma pack(16) on sender and receiver allow POD's to be sent over the network without requiring any marshalling in this case?

UPDATE I use pragma pack for simplicity. I know that each compiler has its own notation. So please do not get exited about this. I also know that its not part of the standard. I know I know . . . just answer the question or downvote. But closing the question is just ridiculous.

Patrick Fromberg
  • 1,313
  • 11
  • 37
  • 4
    Don't rely on alignment and struct layout. Serialize your data into a portable format, then deserialize it on the other end. – Remy Lebeau Dec 19 '17 at 23:51
  • 3
    Not in C++, no. The standard doesn't mandate any particular alignment for any type (except perhaps `char`), and doesn't at all recognize a concept of "endianness", nor "packing". – Igor Tandetnik Dec 19 '17 at 23:52
  • 2
    Also, there are no standard pragmas. –  Dec 20 '17 at 00:21
  • @HansPassant, I have no doubt you have an answer but I cannot decipher your comment, it's a bit too compressed and indirect. What do you mean with passing two values? More stuff with `pack(1)`? What do you mean with reinventing the wheel? reinvent serialization? I am using the above technique already for very high performance communication, but only between x86 machines between programs compiled with the same compiler. – Patrick Fromberg Dec 20 '17 at 00:44
  • If you use `#pragma pack(1)` (or your compiler equivalent), the code might not works in architecture that require proper alignment, might be much slower or unaligned data might not be usable for atomic operations for example. And obviously, if the endianness change, you need to swap bytes as appropriate for integer values. If you use floating point, you have to validate that they use same format... **C++ does not define any binary standard**. Although `struct` and `pragma` might improve performance on some platform, it makes the code less portable. – Phil1970 Dec 20 '17 at 00:51
  • As a side note, whenever one use such "hack" to serialize data, it should add a lot of validation to ensure that everything is saved as intended. Structure size validation is mandatory. You want, the compiler to detect any unexpected size either when defining the structure or when making a newer version of the data. In any case, I would recommend you to define structure that follow natural member alignment as it might make it easier to support on most platform with adequate performance. – Phil1970 Dec 20 '17 at 00:56
  • @Phil1970. Thanks Phil, I assume you are correct and removed the pack(1) and endianess point from the question. – Patrick Fromberg Dec 20 '17 at 00:57
  • For safety, you should always serialize your data to a portable format before sending it over a network. In practice, as long as you don't pack your structs, you're probably safe as long as you take into account endianness and use fixed-width types.... (There's a blog post about a common networking library basically doing this for 20+ years without any issues but I need to find it). But don't rely on it. Especially considering how fast serialization is with libraries like [cereal](https://github.com/USCiLab/cereal). You're better off writing correct code. – Alex Huszagh Dec 20 '17 at 01:08

0 Answers0