How to deallocate variables on the stack?

前端 未结 6 1717
一向
一向 2021-01-29 14:15

Is there a way to deallocate variables and/or objects created on the stack? I am specifically talking about the stack not the heap.

I do not want to debate whether this

6条回答
  •  猫巷女王i
    2021-01-29 15:03

    Every time you return from a function, any memory allocated on the stack by that function is deallocated. If you need to reduce the scope of a variable for some reason, you can just make a new smaller scope by creating a block with {}.

    void function(void)
    {
       int x;
       {
         int y;
       } // scope of y ends
    } // scope of x ends
    

提交回复
热议问题