I call free(), but the pointer still has data and it's content hasn't changed

前端 未结 2 1664
囚心锁ツ
囚心锁ツ 2020-12-22 13:37

The code is below.

My question is about the result. I want to understand, why after calling free(p) p->elem turns to \'0\', but the p->str still contains \"hello\"?

2条回答
  •  一向
    一向 (楼主)
    2020-12-22 13:53

    Freeing memory doesn't actually clear the pointer or the memory it pointed to (except under specialized circumstances meant to assist in debugging problems like this -- never rely on the behavior though). Using a pointer after freeing the memory it pointed to is invalid, and is undefined behavior. It might as well crash, or cause random values to be printed.

    Also, in C you should not cast the return of malloc.

提交回复
热议问题