c++ - Delete Derived Class with Stack Pointer? -
so stack memory cannot manually freed via delete, because somehow induces ub. when creating derived class object, "new"-keyword involved. example:
void some_function(){ base* base; base = new derived; }
as "new" involved, base = new derived located on heap? , if so, memory located, need freed manually again? or located on stack , memory freed after program terminates anyway?
the rule is: any time use new
, must free memory delete
. exception called "placement new
" used (see comments).
in case, pointer called base
located on stack. however, points allocated on heap.
when program terminates, all of memory freed kernel. doesn't matter or how memory allocated.
(note pedants: using "stack" , "heap" here per common usage, notwithstanding fact terms not mentioned in c++ standard.)
Comments
Post a Comment