有没有在C程序中调用'自由'为同一指针两次任何伤害? [英] Is there any harm in calling 'free' for the same pointer twice in a C program?

查看:108
本文介绍了有没有在C程序中调用'自由'为同一指针两次任何伤害?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个C程序,如:

If I have a c program, like:

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

/* do stuff */

free(my_type);

/* do a bunch of more stuff */

free(my_type);

是'自由'的,要求my_type做任何伤害?我调用free(my_type)后,确实指针变成一个空指针再次?

Does the calling of 'free' for my_type do any harm? After I call free(my_type), does the pointer become a null pointer once again?

推荐答案

解除分配与免费的内存区域不会使指针NULL的内容。假设你有为int * A =的malloc(sizeof的(INT)) A 0xdeadbeef 和执行免费(一)然后执行后 A 仍然包含 0xdeadbeef 免费调用这个内存地址后没有给你更多的保留。像你这样的东西已经租下一个单位与使用了一段时间的malloc ,返回平通过免费,那么你可能有一个重复键为平,但它不是为您保留。

Deallocating a memory area with free does not make the contents of the pointer NULL. Suppose that you have int *a = malloc (sizeof (int)) and a has 0xdeadbeef and you execute free (a) then after execution a still contains 0xdeadbeef but after the free call this memory address is no more reserved for you. Something like you have rented a flat with malloc used for some time, returned the flat by free then you might have a duplicate key for the flat, but it is not reserved for you.

做一个免费上会造成双重释放内存损坏已经免费 D内存。

Doing a free on an already freed memory will result in double free memory corruption.

这篇关于有没有在C程序中调用'自由'为同一指针两次任何伤害?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆