title: Are packed structs portable?
body: <p>I have some code on a Cortex-M4 microcontroller and'd like to communicate with a PC using a binary protocol. Currently, I'm using packed structs using the GCC-specific <code>packed</code> attribute.</p>
  
  <p>Here is a rough outline:</p>
  
  <pre><code>struct Sensor1Telemetry {
      int16_t temperature;
      uint32_t timestamp;
      uint16_t voltageMv;
      // etc...
  } __attribute__((__packed__));
  
  struct TelemetryPacket {
      Sensor1Telemetry tele1;
      Sensor2Telemetry tele2;
      // etc...
  } __attribute__((__packed__));
  </code></pre>
  
  <p>My question is:</p>
  
  <ul>
  <li>Assuming that I use the exact same definition for the <code>TelemetryPacket</code> struct on the MCU and the client app, will the above code be portable accross multiple platforms? (I'm interested in x86 and x86_64, and need it to run on Windows, Linux and OS X.)</li>
  <li>Do other compilers support packed structs with the same memory layout? With what syntax?</li>
  </ul>
  
  <p><strong>EDIT</strong>:</p>
  
  <ul>
  <li>Yes, I know packed structs are non-standard, but they seem useful enough to consider using them.</li>
  <li>I'm interested in both C and C++, although I don't think GCC would handle them differently.</li>
  <li>These structs are not inherited and don't inherit anything.</li>
  <li>These structs only contain fixed-size integer fields, and other similar packed structs. (I've been burned by floats before...)</li>
  </ul>