本文介绍了使用realloc释放内存的麻烦的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我的程序有一个指向在本地声明的结构的指针 函数,使用malloc分配内存并重新分配 稍后使用realloc,当我尝试在函数结束时释放内存时,程序会崩溃。在调试时,我发现指针指向的 地址在其中一个 realloc调用期间发生了变化。但根据realloc的文档, 地址的更改是完全正常的。我的一些同事说,释放中的一些错误是由于realloc中的一些错误而导致内存失败的原因。我觉得很难相信,但是b $ b相信。它是依赖于编译器(IDE是MS Visual Studion .NET 2003)还是我的代码可能有问题? 解决方案 可能有您的代码有问题。 尝试将问题提炼为简短但完整的 和可编辑的示例,以显示问题,并发布 那个例子。令人惊讶的是,当我不需要咨询它时,我的水晶球有多好...... - Er ********* @ sun.com 向我们展示最简单的可编辑代码片段,展示了您所描述的 行为 - 否则*任何人*如何*告诉我们发生了什么。 请注意,然而,与malloc / realloc / free相关的失败通常是由于您的免费商店损坏,通常是因为在分配的内存范围之外写了。 HTH, - g - Artie Gold - 德克萨斯州奥斯汀 http://it-matters.blogspot.com (新帖子12/5) http://www.cafepress.com/goldsays Hi,My program has a pointer to a structure declared locally inside afunction to which memory is allocated using malloc and reallocatedlater using realloc, and when I try to free the memory at the end ofthe function, the program breaks down. While debugging, I find that theaddress to which the pointer is pointing is changed during one of therealloc calls. But as per the documentation on realloc, the change ofaddress is perfectly normal. Some of my colleagues say the failure in''free''ing the memory is due to some bug in ''realloc''. I find it hard tobelieve though. Is it dependent on the compiler (the IDE is MS VisualStudion .NET 2003) or could there be something wrong with my code? 解决方案There could be something wrong with your code.Try distilling the problem down to a short but completeand compilable example that shows the trouble, and postthat example. It is astonishing how much better my crystalball works when I don''t need to consult it ...-- Er*********@sun.comShow us the smallest compilable snippet of code that exhibits thebehavior you describe -- otherwise how could *anyone* tell what''s going on.Be advised, however, that failures related to malloc/realloc/free areusually due to the corruption of your free store, usually due to writingoutside the bounds of allocated memory.HTH,--ag--Artie Gold -- Austin, Texas http://it-matters.blogspot.com (new post 12/5) http://www.cafepress.com/goldsays 这篇关于使用realloc释放内存的麻烦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!