title: Is C++ memory alignment correct or inefficient?
body: <p>I tested this code just trying to find out how much memory c++ actually reserved for the new operator. </p>
  
  <pre><code>#include&lt;iostream&gt;
  using namespace std;
  int main() {
  
    cout &lt;&lt; "alignment of " &lt;&lt; alignof(int) &lt;&lt; endl;
    int *intP1 = new int;
    *intP1 = 100;
    cout &lt;&lt; "address of intP1 " &lt;&lt; intP1 &lt;&lt; endl;
    int *intP2 = new int;
    *intP2 = 999;
    cout &lt;&lt; "address of intP2 " &lt;&lt; intP2 &lt;&lt; endl;
    int *intP3 = new int;
    cout &lt;&lt; "address of intP3 " &lt;&lt; intP3 &lt;&lt; endl;
    *intP3 = 333;
  
    cout &lt;&lt; endl;
    cout &lt;&lt; (reinterpret_cast&lt;char *&gt;(intP3)-reinterpret_cast&lt;char *&gt;(intP2)) &lt;&lt; endl;
    cout &lt;&lt; intP3-intP2 &lt;&lt; endl;
    cout &lt;&lt; endl;
  
    cout &lt;&lt; *(intP1) &lt;&lt; endl;
    cout &lt;&lt; *(intP1+4) &lt;&lt; endl;
    cout &lt;&lt; *(intP1+8) &lt;&lt; endl;
    cout &lt;&lt; *(intP1+16) &lt;&lt; endl;
    delete intP1;
    delete intP2;
    delete intP3;
    return 0;
  }
  </code></pre>
  
  <p>After compiled the code with -std=c++11 flag and ran it, here is what I got from a x86_64 machine. </p>
  
  <pre><code>    alignment of int4
      address of intP1 = 0xa59010
      address of intP2 = 0xa59030
      address of intP3 = 0xa59050
  
      the distance of intP3 and intP2 = 32
  
      intP1 value = 100
      is this a padding value = 0
      intP2 value = 999
      intP3 value = 333
  </code></pre>
  
  <p>It seems that when using new to allocate a 4 bytes memory for an integer, it actually reserved 32 bytes block which is the total space for 8 integers. According to the explanation of the c++ alignment, for 64 bit machine, memory is aligned on 16 bytes, why the distance here is 32 bytes?</p>
  
  <p>Could some one help me to sort this out? Thanks in advance. </p>