关于在运行时释放内存 [英] Regarding freeing memory at run time

查看:123
本文介绍了关于在运行时释放内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


感谢您的时间。


我们有控制台应用程序,我们在运行时使用分配两个缓冲区malloc API。


在退出应用程序时,我们使用free释放内存。


在运行时分配缓冲区调用免费API后,如果我们尝试访问我们仍然可以访问它的缓冲区。


指针在VS2005调试器中没有显示为坏指针。当我们自由运行时,指针1上的内存被释放,内存窗口显示已分配内存的问号。


如何通过调用免费API来确保释放内存? / p>

前两个图像对应于buffer1。第一个是在调用malloc之前,第二个图像是在gpstrNibpApp-> pu8NibpMsg上调用免费API后捕获的。




如果您有任何建议,请与我们联系.Regards,


GSR

解决方案

函数free()将内存释放回分配器,以便在其他人执行malloc以获取内存时它可以重用此区域。 / p>

释放后,内存中的数据仍然存在,所以如果你有指向该区域的指针你可以使用它,但行为是不可预测的,因为使用free你声明该区域是免费的因此操作系统可以在您访问时使用它。



Hi,

Thanks for your time.

We have console application in which we are allocating two buffers at run time using malloc API.

While the exiting the application we are deallocating the memory using free.

After calling the free API on run time allocated buffer, if we try to access the buffer we are still able to access it.

The pointer is not shown as bad pointer in VS2005 debugger. Where as on pointer1 when we run free the memory is freed and the memory window displays question marks for the allocated memory.

How to make sure that the memory is freed by calling free API?

The first two images correspond to buffer1. The first one is before calling malloc and the second image is captured after calling free API on the gpstrNibpApp->pu8NibpMsg.

Please let me know if you have any suggestions.Regards,

GSR

解决方案

The function free() releases the memory back to the allocator so that it can reuse this area if someone else execute a malloc to have memory.

After free, the data in memory is still there so that if you have the pointer to that area you can use it but the behaviour is unpredictable because using free you have declared that the area is free so the operating system can use it while you are accessing.


这篇关于关于在运行时释放内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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