在C程序中为同一个指针调用'free'是否有任何伤害?

Ash*_*Ash 11 c memory-management

如果我有交流计划,比如:

SomeTypePtr my_type;
my_type = malloc(sizeof(someType));

/* do stuff */

free(my_type);

/* do a bunch of more stuff */

free(my_type);
Run Code Online (Sandbox Code Playgroud)

为my_type调用'free'会不会造成任何伤害?在我调用free(my_type)之后,指针再次成为空指针吗?

pho*_*xis 12

取消分配内存区域free不会使指针的内容为NULL.假设您拥有int *a = malloc (sizeof (int))a拥有0xdeadbeef并且执行free (a)后执行a仍然包含0xdeadbeef但在free调用之后此内存地址不再为您保留.像你这样的东西已经租了malloc一段时间用了一段时间,free然后退回公寓,你可能有一个重复的公寓钥匙,但它不是为你保留的.

做一个free对已经freed内存将导致双重释放内存损坏.