title: Memory Organization in C++
body: <p>I have been reading about how memory is allocated in C++.</p>
  
  <p>A few resources to mention:</p>
  
  <p><a href="http://www.geeksforgeeks.org/memory-layout-of-c-program/" rel="nofollow noreferrer">http://www.geeksforgeeks.org/memory-layout-of-c-program/</a></p>
  
  <p><a href="http://duartes.org/gustavo/blog/post/anatomy-of-a-program-in-memory" rel="nofollow noreferrer">http://duartes.org/gustavo/blog/post/anatomy-of-a-program-in-memory</a></p>
  
  <p><a href="https://stackoverflow.com/questions/10157122/object-creation-on-the-stack-heap">Object creation on the stack/heap?</a></p>
  
  <p><a href="https://stackoverflow.com/questions/1169858/global-memory-management-in-c-in-stack-or-heap">Global memory management in C++ in stack or heap?</a></p>
  
  <p><a href="http://msdn.microsoft.com/en-us/library/vstudio/dd293645.aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/vstudio/dd293645.aspx</a></p>
  
  <p><a href="https://stackoverflow.com/questions/1952019/heap-stack-and-multiple-processes">Heap / Stack and multiple processes</a></p>
  
  <p><a href="https://stackoverflow.com/questions/7252883/do-different-programs-gets-their-memory-from-a-common-heap-or-from-a-separate-he">Do different programs gets their memory from a common heap or from a separate heap?</a></p>
  
  <p><a href="http://computer.howstuffworks.com/c28.htm" rel="nofollow noreferrer">http://computer.howstuffworks.com/c28.htm</a></p>
  
  <p><img src="https://i.stack.imgur.com/okF8m.gif" alt="enter image description here"></p>
  
  <p>I want to clarify a few points based on my reading:</p>
  
  <p><strong>According to <a href="http://www.geeksforgeeks.org/memory-layout-of-c-program/" rel="nofollow noreferrer">http://www.geeksforgeeks.org/memory-layout-of-c-program/</a> Section 4 Stack "Stack, where automatic variables are stored, along with information that is saved each time a function is called"</strong> </p>
  
  <p>Suppose:</p>
  
  <pre><code>class myClass{
    int a;
    char b;
    public:
    myClass(int a,char b)
    {
     this-&gt;a = a;
     this-&gt;b = b;
    } 
  };
  </code></pre>
  
  <p>1)According to what I have read, when we compile this code the binary sits in program memory and nothing has been allocated on stack yet. Correct?</p>
  
  <p>Now in my main:</p>
  
  <pre><code>int main()
  {
   myClass Ob(1,'c');
   return 0;
  } 
  </code></pre>
  
  <p>2) Now an object Ob of size 5 bytes (4 bytes (int), 1 byte (char) - 32 bit OS) is created on stack, since it is an automatic variable. Correct ?</p>
  
  <p>3) When the constructor <code>myClass(int a,char b)</code> is called do the temporary variables (parameters a, b) are created on stack for constructor and then destroyed after creating the object Ob? Like when we call a function by passing parameters by value.</p>
  
  <p>Now suppose another class</p>
  
  <pre><code>class pointerClass {
   int a;
   char* b;
   public:
   pointerClass(int size){
   b= new char[size];
   a=size;
   }
  }; 
  </code></pre>
  
  <p>Now in main :</p>
  
  <pre><code>int main()
  {
   pointerClass ptr(10) ; //Step 1
  }
  </code></pre>
  
  <p>4)  Does this mean ptr object of size 8 bytes ( int a (4 bytes) , char* b (4 bytes i.e. this is just holding an address pointing to heap ) is created on stack ? Further a memory of 10 bytes (corresponding to new char[10] is allocated on heap) which is being pointed by the content of char* b? Am I correct? </p>
  
  <p>5)When we pass a parameter to a function by reference such as <code>fn (int *a,char* b)</code> or <code>fn(int&amp; a,char&amp; b)</code> does this mean that a temporary pointer/reference is created on stack for the function which points to the actual object being passed and destroyed when the function returns? or rather the actual object is passed instead of creating and destroying a temporary pointer/reference on stack for the function?</p>
  
  <p>This I asked yesterday but I am not satisfied with the answer:
  <a href="https://stackoverflow.com/questions/16391863/constructor-copy-constructor-and-stack-creation-c">Constructor, Copy Constructor and Stack Creation : C++</a></p>
  
  <p>6)When we overload a fn such as <code>fn(int a,char b)</code> <code>fn(int&amp; a,char&amp; b)</code> we can call from main as <code>fn(A,B)</code>
  with below cast
  <code>static_cast&lt;void(*)(int, char)&gt;(fn)(a, c); //Calls fn(int a,char b)</code>
  <code>static_cast&lt;void(*)(int&amp;, char&amp;)&gt;(fn)(a, c);//Calls fn(int&amp; a.char&amp; b)</code>
  What exactly is happening here ? What is void (*) .</p>
  
  <p>Thanks</p>