title: Is it safe to cast a structure with virtual functions over a byte array?
body: <p>For instance, I have a structure definition</p>
  
  <pre><code>struct Data { 
      uint8_t data1; 
      uint16_t data2;
      virtual uint8_t getData1() { return data1; }
      virtual uint16_t getData2() { return data2; } 
  }
  </code></pre>
  
  <p>I have a byte array</p>
  
  <pre><code>uint8_t data[3];</code></pre>
  
  <p>Is it safe to do this:</p>
  
  <pre><code>Data *d = (Data*)data;</code></pre>
  
  <p>I am asking because, i read that a class with virtual functions store
  a virtual table pointer and there's no standard defining it where it
  is stored in the object. Also, If I inherit from Data, for instance</p>
  
  <pre><code>struct Data2 : Data { uint8_t data3; 
  virtual uint8_t getData3() { return data3; } }</code></pre> 
  
  <p>What can be the order in which member variables in the object of Data2
  are stored? If I cast Data2 structure over a byte array would it be in
  the order data1, data2, data3? Thank you in advance.</p>