I am getting a warning in my code:
warning: taking address of temporary
I have seen similar questions, but they do not answer my specific problem.
Here is what my code is doing:
vector<A*>* ex_A;
ex_A->push_back( &A()); //I get this warning taking address of temporary
Is this undefined behavior?
I did have this before, which was fine, but i didn’t want to worry about deleting memory from the heap.
vector<A*>* ex_A;
ex_A->push_back( new A());
Could some one explain to me what the warning means?
&A()is creating a temporary object which gets destructed on exit of the full expression automagically, whilenew A()creates a new object on the heap which will live until you manually destroy it usingdelete.I should add that if you store objects allocated with new in your
vector<A*>and the vector gets destructed, the objects stored inside will not get deleted automatically, thus you will have a memory leak. You can verify this by usingvalgrind --leak-check=full my_compiled_programon your program, which generally is a good idea.