Calling delete on variable allocated on the stack

前端 未结 11 2036
遇见更好的自我
遇见更好的自我 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条回答
  •  Happy的楠姐
    2020-11-22 14:49

    After playing a bit with g++ 4.4 in windows, I got very interesting results:

    1. calling delete on a stack variable doesn't seem to do anything. No errors throw, but I can access the variable without problems after deletion.

    2. Having a class with a method with delete this successfully deletes the object if it is allocated in the heap, but not if it is allocated in the stack (if it is in the stack, nothing happens).

提交回复
热议问题