Calling delete on variable allocated on the stack

前端 未结 11 1941
遇见更好的自我
遇见更好的自我 2020-11-22 14:35

Ignoring programming style and design, is it \"safe\" to call delete on a variable allocated on the stack?

For example:

   int nAmount;
   delete &am         


        
11条回答
  •  难免孤独
    2020-11-22 15:13

    here the memory is allocated using stack so no need to delete it exernally but if you have allcoted dynamically

    like int *a=new int()

    then you have to do delete a and not delete &a(a itself is a pointer), because the memory is allocated from free store.

提交回复
热议问题