title: Stack Frame Question: Java vs C++
body: <p>Q1. In Java, all objects, arrays and class variables are stored on the heap? Is the same true for C++? Is data segment a part of Heap?</p>
  
  <p>What about the following code in C++?</p>
  
  <pre><code>class MyClass{
      private:
              static int counter; 
              static int number;
  };
  
  MyClass::number = 100;
  </code></pre>
  
  <p>Q2. As far as my understanding goes, variables which are given a specific value by compiler are stored in data segment, and unintialized global and static variables are stored in BSS (Block started by symbol). In this case, MyClass::counter being static is initialized to zero by the compiler and so it is stored at BSS and MyClass::number which is initialized to 100 is stored in the data segment. Am I correct in making the conclusion?</p>
  
  <p>Q3. Consider following piece of codes:</p>
  
  <pre><code>void doHello(MyClass &amp;localObj){   
  // 3.1 localObj is a reference parameter, where will this get stored in Heap or Stack?
        // do something
  }
  
  void doHelloAgain(MyClass localObj){   
  // 3.2 localObj is a parameter, where will this get stored in Heap or Stack?
        // do something
  }
  
  int main(){
        MyClass *a = new MyClass();  // stored in heap
  
        MyClass localObj;   
        // 3.3 Where is this stored in heap or stack?
        doHello(localObj);
        doHelloAgain(localObj);
  }
  </code></pre>
  
  <p>I hope I have made my questions clear to all</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>Please refer this <a href="http://linuxgazette.net/112/krishnakumar.html" rel="nofollow noreferrer">article</a> for some understanding on BSS</p>
  
  <p><strong>EDIT1:</strong> Changed the class name from MyInstance to MyClass as it was a poor name. Sincere Apologies</p>
  
  <p><strong>EDIT2:</strong> Changed the class member variable number from non-static to static</p>