c++ - Memory allocation confusion -


I created a class A and wrote the following function foo ()

  class A {public: int a; }; A * Fu (AA; Return & amp; A1; } Int main () {A * a2; A2 = foo (); Return 0; }  

The compiler gave me a warning because a1 is a local variable and I'm referring it to the stack (hence its value may change unexpectedly).

Now I changed foo () to

  a * foo () {a a1; A * A3; A3 = & amp; A1; Return A3; }  

The compiler no longer gives any warnings. Is it because A3 is built on the heap? If so, the indicators are always made on the heap like this. I thought that the heap is used only by the new / maulok.

Now the compiler has any warning.

The compiler is not giving any alerts because you have added enough complexity to make the analysis fool, which does your code.

You are still returning an indicator in a local variable, and you can not use that pointer after the function returns.


Comments

Popular posts from this blog

paypal - How to know the URL referrer in PHP? -

oauth - Facebook OAuth2 Logout does not remove fb_ cookie -

wpf - Line breaks and indenting for the XAML of a saved FlowDocument? -