否 - Nick Keighley 我不会吸毒,我的梦想令人恐惧足够。 --M。 C. Escher 默认情况下指向NULL或执行free()。我赞推荐这种结构: 免费(p),p = NULL; 它让它变得简单测试,发生免费(NULL)定义和 无害。 - A + Emmanuel Delahaye 首先,它是未定义的行为,所以_anything_可能发生。特别是, 每当你这样做的时候它可能会无害地返回 开发你的程序,并且第一次认真使用它 可能会使正在运行的计算机崩溃并造成相当大的财务损失。不要这样做。 究竟发生了什么取决于你的实施。 一些通过调用malloc初始化的指针变量,可能非常巧合。 未初始化指针变量的值。在那个 的情况下,调用free()将释放该指针,可能会造成有害的后果。这并不像看起来那样不可能,因为一个 未初始化的变量可能会保存在一个寄存器中,而这个变量恰好包含另一个完全不相关的调用函数变量。 /> Hi,If I call free() with a uninitialised pointer, will the programstate become undefined, or will free() return harmlessly?Incidentally, is there a way in Standard C to determineweather a pointer points to a valid block of allocatedmemory?Thanks for your time. 解决方案no--Nick KeighleyI don''t use drugs, my dreams are frightening enough. --M. C. EscherMake it point to NULL by default or once free() has been performed. Irecommend this construction :free(p), p = NULL;It makes it easy to test, and it happens that free (NULL) is defined andharmless.--A+Emmanuel DelahayeFirst, it is undefined behavior, so _anything_ can happen. Especially,it might return harmlessly every time you do this while you aredevelopping your program, and the first time it is used seriously itcould crash the computer it is running on and cause considerablefinancial damage. Don''t do this.What exactly happens depends on your implementation. The value of anuninitialised pointer variable might by great coincidence be the same assome pointer variable that was initialised by calling malloc. In thatcase, calling free () will free that pointer, with likely harmfulconsequences. That is not as unlikely as it seems, because anuninitialised variable might be kept in a register that happens tocontain another completely unrelated variable of the calling function. 这篇关于在未初始化的指针上调用free()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-23 15:12