恰好通过自由释放内存中的数据的内容()? [英] What happens to the data in memory deallocated by free()?

查看:113
本文介绍了恰好通过自由释放内存中的数据的内容()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正好是在一个内存位置present刚刚被释放的数据多么免费()?是,数据还删除,存储现在有一个垃圾的价值?或该数据仍然存在那里,直到一个新的数据被存储在该存储器位置(将来)?

我的意思是,对于低于code:

 为int * PTR;
PTR =的malloc(sizeof的(INT));
* PTR = 1;
 //假设PTR = 2000
 //现在免​​费
免费(PTR);
//我的问题是什么是存储在内存中的地址2000现在的价值?
//这还算是'1'或一些垃圾值?


解决方案

结果是未predictable。有可能发生几个选项。问题的关键是,你不能依赖于免费发布存储器的任何行为()

一些例子:


  • 的内存可以不变(保持不变,因为它是使用相同的数据)。

  • 它可以给另一个存储器分配,在这种情况下,它可以在任何时候被覆盖。

  • 可以归零。

  • 包含内存页面可以被返回到操作系统,从进程的内存映射删除它,使你的程序崩溃,如果您尝试访问它。

What happens to the data that is present in a memory location that has just been freed by a free() ? Is that data also deleted and the memory will now have a garbage value ? Or that data still persists there untill a new data is stored in that memory location (in future) ?

I mean, for code below:

int *ptr;
ptr = malloc(sizeof(int));
*ptr = 1;
 // Suppose ptr = 2000
 //Free now
free(ptr);
// My question is what is the value stored in memory address 2000 now ?
// Is it still '1' or some garbage value ?

解决方案

The result is unpredictable. There are several options that can happen. The point is that you cannot rely on any behavior of the memory released by free()

Some examples:

  • the memory can be untouched (remain the same as it is with the same data).
  • It can be given to another memory allocation, in which case it can be written over at any point.
  • It can be zeroed.
  • The page containing the memory can be returned to the OS, removing it from the memory map of your process, making your program crash if you try to access it.

这篇关于恰好通过自由释放内存中的数据的内容()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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