NULL pointer is the same as deallocating it?

前端 未结 11 2539
一个人的身影
一个人的身影 2021-02-18 18:29

I was working on a piece of code and I was attacked by a doubt: What happens to the memory allocated to a pointer if I assign NULL to that pointer?

For instance:

11条回答
  •  伪装坚强ぢ
    2021-02-18 18:51

    Variables stored on the stack, are the local variables of each function, e.g. int big[10]; Variables stored on the heap, are the variables which you initiated using explicit memory allocation routines such as malloc(), calloc(), new(), etc.

    Variables stored on the stack have a lifetime that is equal to the lifetime of the current stack frame. In English, when the function returns, you can no longer assume that the variables hold what you expect them to hold. That's why its a classic mistake to return a variable that was declared local in a function.

提交回复
热议问题