Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><a href="http://www.cppreference.com/wiki/keywords/sizeof" rel="noreferrer"><code>sizeof(x)</code></a> returns the amount of memory (in bytes) that the variable or type <code>x</code> occupies. It has nothing to do with the value of the variable.</p> <p>For example, if you have an array of some arbitrary type <code>T</code> then the distance between elements of that array is exactly <code>sizeof(T)</code>.</p> <pre><code>int a[10]; assert(&amp;(a[0]) + sizeof(int) == &amp;(a[1])); </code></pre> <p>When used on a variable, it is equivalent to using it on the type of that variable:</p> <pre><code>T x; assert(sizeof(T) == sizeof(x)); </code></pre> <p>As a rule-of-thumb, it is best to use the variable name where possible, just in case the type changes:</p> <pre><code>int x; std::cout &lt;&lt; "x uses " &lt;&lt; sizeof(x) &lt;&lt; " bytes." &lt;&lt; std::endl // If x is changed to a char, then the statement doesn't need to be changed. // If we used sizeof(int) instead, we would need to change 2 lines of code // instead of one. </code></pre> <p>When used on user-defined types, <code>sizeof</code> still returns the amount of memory used by instances of that type, but it's worth pointing out that this does not necessary equal the sum of its members.</p> <pre><code>struct Foo { int a; char b; }; </code></pre> <p>While <code>sizeof(int) + sizeof(char)</code> is typically <code>5</code>, on many machines, <code>sizeof(Foo)</code> may be <code>8</code> because the compiler needs to <a href="http://en.wikipedia.org/wiki/Data_structure_alignment" rel="noreferrer">pad out</a> the structure so that it lies on 4 byte boundaries. This is not always the case, and it's quite possible that on your machine <code>sizeof(Foo)</code> will be 5, but you can't depend on it.</p>
    singulars
    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