title: Get byte representation of C++ class
body: <p>I have objects that I need to hash with SHA256. The object has several fields as follows:</p>
  <pre><code>class Foo {
      // some methods
      protected:
         std::array&lt;32,int&gt; x;
         char y[32];
         long z;
  }
  </code></pre>
  <p>Is there a way I can directly access the bytes representing the 3 member variables in memory as I would a struct ? These hashes need to be computed as quickly as possible so I want to avoid malloc'ing a new set of bytes and copying to a heap allocated array. Or is the answer to simply embed a struct within the class?</p>
  <p>It is critical that I get the exact binary representation of these variables so that the SHA256 comes out exactly the same given that the 3 variables are equal (so I can't have any extra padding bytes etc included going into the hash function)</p>