重用使用C释放的指针 [英] Reusing freed pointers in C

查看:254
本文介绍了重用使用C释放的指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有这个网站上的许多问题就在使用后释放的指针,并进一步将它们设置为NULL。参数是激烈的,主题看似平分。例如:<一href=\"http://stackoverflow.com/questions/1879550/should-one-really-set-pointers-to-null-after-freeing-them\">This问题。
我感到困惑的一般释放的指针。

假设你有一个指向一些内存空间。使用空间后,你释放指针,但不要将其设置为NULL。后来,你有另一个指针调用malloc(),或一些模拟,并分配内存,包括早期释放(即原来的指针仍然指向)的内存。如果这个新的指针在该内存块写操作,会发生什么? Ituitively什么都不会发生,但是在早期提供的链接OP写道,它会引起程序崩溃。


所以我的问题是:

1]给定一个释放的指针,是什么让你从reassiging该指针到一个新的存储位置?为什么是坏的做法重用释放指针?如果调用free(PTR)仅返回此内存到操作系统,为什么你能不能重新分配的指针,因此其他的内存位置和重用呢?

 的char * PTR =的malloc(sizeof的(* PTR)); //第一次分配
免费(PTR); //释放内存
PTR = NULL;
PTR =的malloc(sizeof的(* PTR)); //重新分配

2]为什么会写一个内存块,这是previously释放,仍然有原来的指针指向它,导致程序崩溃? - 看看第一个帖子上面链接(如果我missinter PTED这一段的意图$ P $问题的第一段,请解释一下,因为它不是在该指针是否被再次用来写内存或新的明确的指针被创建。)


解决方案

  

给定一个释放的指针,是什么让你从reassiging该指针到一个新的存储位置?


从技术上说,没什么。你甚至都不需要设置 PTR = NULL 在释放和重新分配指针之间。当释放并重新分配由code其他行分隔,但是,指针设置为 NULL 可能略有提高可读性。


  

为什么会写一个内存块,这是previously释放,仍然有原来的指针指向它,导致程序崩溃?


只要保存一个指向通过另一个指针访问的内存块是绝对OK,只要你的程序不尝试非关联化的指针。不幸的是,即使你取消引用释放的指针,它不一定会导致你的程序崩溃:往往不是,这样的行为会被忽视。它仍然是一个未定义的行为,虽然。你的程序的其他部分可能已写入的数据与你所期望的不兼容,在这种情况下,你会看到错误是非常难以找到或解释。

There are many questions on this website regarding freeing pointers after use and, further, setting them to NULL. Arguments are fierce and the topic is seemingly divided equally. For example: This question. I am confused about freeing pointers in general.

Imagine you have a pointer to some memory space. After using the space, you free the pointer but do not set it to NULL. Later, you have another pointer that calls malloc(), or some analog, and is is allocated memory including the memory freed earlier(that the original pointer still points to). If this new pointer writes in this memory block, what happens? Ituitively nothing would happen, but the OP in the link provided earlier writes that it would crash the program.


So my questions are:

1] Given a freed pointer, what is keeping you from reassiging that pointer to a new memory location? Why is it 'bad' practice to reuse freed pointers? If calling free(ptr) only returns this memory to the OS, why can you not reassign the pointer so other memory locations and reuse it?

char *ptr = malloc(sizeof(*ptr)); //first allocation
free(ptr); //release memory 
ptr = NULL; 
ptr = malloc(sizeof(*ptr)); //reallocate

2] Why would writing to a memory block that was previously freed, that still has the original pointer to it, cause the program to crash? -- See the first paragraph of the first post to the question linked above (if I missinterpreted the intent of this paragraph, please explain because it is not explicit on whether that pointer is used again to write the memory or a new pointer is created.)

解决方案

Given a freed pointer, what is keeping you from reassiging that pointer to a new memory location?

Technically, nothing. You do not even need to set ptr = NULL in between of freeing and re-assigning the pointer. When freeing and re-assigning are separated by other lines of code, however, setting the pointer to NULL may improve readability slightly.

Why would writing to a memory block that was previously freed, that still has the original pointer to it, cause the program to crash?

Simply holding a pointer to a block of memory accessible through another pointer is absolutely OK, as long as your program does not try dereferencing that pointer. Unfortunately, even if you dereference the freed pointer, it would not necessarily cause your program to crash: more often than not, such behavior would go unnoticed. It remains an undefined behavior, though. Another part of your program may have written data incompatible with what you expect, in which case you will see bugs that are extremely hard to find or explain.

这篇关于重用使用C释放的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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