Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>.size()</code> can return anything, depends on how it is implemented. </p> <p>Anyway, <code>.size()</code> <strong>usually</strong> returns the size of an <strong>container</strong>, in other words - the number of elements, that the container contains.</p> <p>The <code>sizeof</code> operator is integrated operator, that returns the number of bytes, allocated on the stack. For example</p> <pre><code> char a[50]; char* b = new char[50]; cout &lt&lt sizeof( a ) &lt&lt '\n'; cout &lt&lt sizeof( b ); </code></pre> <p>prints 50 (which is <code>50 * sizeof( char ) = 50 * 1 = 50</code> ), and the second line prints 8 - as this is the size of the pointer (<strong>my machine is x64</strong>, that's why it's 8, if I had 32bit CPU, it would be 4).</p> <p><code>cout &lt;&lt; sizeof( *b );</code> would print 1, as <code>*b</code> dereferences the pointer and returns the first element of the array (it's the same as <code>sizeof( b[0] )</code> which is <code>sizeof( char )</code> )</p> <p>In other words, you'd better rely on <code>.size()</code> if you want to see the number of elements, if it's a container and if it provides such method, of course.</p> <hr> <p>Other example:</p> <pre><code>class A { int a; char b[100]; }; class B { int a; char* b; public: B() { b = new char[100]; } ~B() { delete[] b; } }; int main() { cout &lt&lt sizeof( A ) &lt&lt '\n'; cout &lt&lt sizeof( B ) &lt&lt '\n'; B b; cout &lt&lt sizeof( b ) &lt&lt '\n'; return 0; }</code></pre> <p>The first one, on a x64 plarform, will print 104, as we have <code>100*sizeof(char) + sizeof(int) (=4 )</code> (<strong>note</strong> <code>104 mod 8 == 0</code>, you'll see why ); </p> <p>The second one and the third one will print the same. <strong>But note</strong>, we have alignment here! On x64, the alignment is on 8B, so <code>sizeof( char * )</code> gives 8, <code>sizeof( int )</code> gives 4 and this makes 12. <strong>BUT</strong> <code>12 mod 8 == 4</code> which is != 0, so because of the alignment, <code>sizeof( B )</code> prints <strong>16</strong> ( 16 mod 8 == 0 ).</p> <p>So, when you use <code>sizeof</code>, be very careful..<br> Hope that helps (:</p> <hr> <p>For your question about list, take a look at this:</p> <pre><code> list&lt int &gt lint; list&lt char &gt lchar; cout &lt&lt sizeof( lint ) &lt&lt '\n'; cout &lt&lt lint.size() &lt&lt '\n'; cout &lt&lt sizeof( lchar ) &lt&lt '\n'; lint.push_back( 10 ); lint.push_back( 10 ); cout &lt&lt lint.size() &lt&lt '\n'; cout &lt&lt sizeof( lint ) &lt&lt '\n'; </code></pre> <p>All operators <code>sizeof</code> will print the same, depends on the implementation of <code>std::list</code>. But the first <code>.size()</code> will return 0, as there're no elements, and the second one will return 2, as we have 2 elements in the list.</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. 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.
    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