Memory allocation confusion
<p>I created a class A and wrote the following function foo()</p>

<pre><code>class A
{
public:
int a;
};

A * foo()
{
A a1;
return &amp;a1;
}

int main()
{
A * a2;
a2 = foo();
return 0;
}
</code></pre>

<p>The compiler gave me a warning as a1 is a local variable and I am returning its address from the stack (so its value can change unpredictably).</p>

<p>Now I changed foo() to the following</p>

<pre><code>A * foo()
{
A a1;
A *a3;
a3 = &amp;a1;
return a3;
}
</code></pre>

<p>Now the compiler does not give any warning. Is this because a3 is created on the heap? If so are pointers always created on heap like this. I thought heap is utilized only through new/malloc.</p>
