title: C++: Objects allocated on heap
body: <p>When we create an object using  <em>new</em> it is allocated on the heap. But, what happens to the members of the class that we are instantiating? For example, </p>
  
  <pre><code>class foo {
   Bar x;
   Bar *y;
  
   foo() {
     x = 10;
     y = new Bar();
   }
  }
  </code></pre>
  
  <p>Here, x is an object while y is an instance of Bar. Are they both allocated on the heap? So if an object of foo <em>F</em> is created locally inside a method, what will happen to <em>y</em> when F goes out of scope?</p>
  
  <p>Also, if <em>F</em> is created on the heap, when will we conclude that <em>F</em> is dangling (no one pointing to it)? Because, there may be no references to F but there may be references to <em>Y</em>.</p>