Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There's a lot you need to learn here.</p> <ul> <li>First of all, you're treating a structure as an array of bytes. This is strictly undefined behavior due to the <a href="http://cellperformance.beyond3d.com/articles/2006/06/understanding-strict-aliasing.html" rel="nofollow noreferrer">strict aliasing rule</a>. Anything can happen. So don't do it. Use proper serialization (for example via <a href="http://www.boost.org/doc/libs/1_43_0/libs/serialization/doc/index.html" rel="nofollow noreferrer">boost</a>) instead. Yes, it's tedious. Yes, it's necessary.</li> <li>Even if you ignore the undefinedness, and choose to become dependant on some particular compiler implementation (which may change even in the next compiler version), there's still reasons not to do it. <ul> <li>If you save a file on one machine, then load it on another, you may get garbage, because the second machine uses a different float representation, or a different <a href="http://en.wikipedia.org/wiki/Endianness" rel="nofollow noreferrer">endianness</a>, or has different <a href="http://en.wikipedia.org/wiki/Data_alignment" rel="nofollow noreferrer">alignment rules</a>, etc.</li> <li>If your struct contains any pointers, it's very likely that saving them verbatim then loading them back will result in an address that doesn't not point to any meaningful place.</li> </ul></li> <li>Typically when you add a member function, this happens: <ul> <li>the function's machine code is stored in a place shared by all the class instances (it wouldn't make sense to duplicate it, since it's logically immutable)</li> <li>a hidden "this" pointer is passed to the function when it's called, so it knows which object it's been called on.</li> <li>none of this requires any storage space in the instances.</li> </ul></li> <li>However, when you add at least <em>one</em> virtual function, the compiler typically needs to also add a data chunk called a <a href="http://en.wikipedia.org/wiki/Vtable" rel="nofollow noreferrer">vtable</a> (read up on it). This makes it possible to call different code depending on the current runtime type of the object (aka polymorphism). So the first virtual function you add to the class likely <em>does</em> increase the object size.</li> </ul>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload