如何免费()的作品? [英] how free() works?
问题描述
的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&string.h中GT;诠释的main()
{
INT * PTR;
的printf(之前的malloc指针:%P \\ N,PTR);
的printf(之前的malloc VALU为:%d个\\ N,* PTR);
PTR =的malloc(sizeof的(INT));
的printf(malloc的指针后%P \\ N,PTR);
的printf(之后的malloc VALU为:%d个\\ N,* PTR);
INT夹具= 32;
* PTR =夹具;
的printf(之后分配VALU为:%d个\\ N,* PTR);
免费(PTR);
的printf(后免费%P \\ N,PTR); //自由指针后持有理智的地址,然后
的printf(之后费为%d \\ n,* PTR); //为什么coudnt打印?
返回0;
}
输出是:
之前malloc的指针是:0x6edff4
VALU的malloc之前:7265660
后malloc的指针0x9a52008
malloc的VALU后:0
分配VALU后:32
后免费0x9a52008
之后,费0
免费指针仍然认为,内存的地址后,那么我们为什么不能打印内存中的价值。??
什么呢免费()怎么办?
它只是使所有的内存为0 ??
免费指针仍然认为,内存的地址后,那么我们为什么不能打印内存中的价值。??
块引用>由于内存不再属于您。您
免费
D它,这意味着操作系统允许重用但是它认为合适的,无论它需要分配更多的内存。你不再拥有它,因此你不再有任何业务望着由存储器中的数据的价值。还要注意的是:
为int * PTR;
...
的printf(以前的malloc VALU为:%d个\\ N,* PTR);同样无效。
PTR
持有垃圾的价值,并且可以在任何地方点。取消引用它不能保证你可以访问一个内存位置。这两种情况下,调用的未定义行为的,这意味着标准说,不这样做,如果你忽视了你的标准code将在可怕的方式划分,只要您老板正在寻找。
什么呢免费()怎么办?
它只是使所有的内存为0 ??
块引用>不,不一定。操作系统往往置零未使用的内存在后台进行释放calloc 更快,但
免费
只告诉操作系统调用我这个内存中完成,做任何你需要它。操作系统通常会更新一些日常的数据表明,内存块不再由一个进程所拥有的,这样就
的malloc以后调用
可以使用它,如果它是必要的。#include <stdio.h> #include <stdlib.h> #include <string.h> int main () { int * ptr; printf("before malloc pointer is :%p \n",ptr); printf("before malloc valu is :%d \n",*ptr); ptr = malloc(sizeof(int)); printf("after malloc pointer is %p \n",ptr); printf("after malloc valu is :%d \n",*ptr); int jig=32; *ptr = jig; printf("after assignment valu is : %d\n",*ptr); free(ptr); printf("after free %p \n",ptr); // after free pointer holds sane address then printf("after fee is %d\n",*ptr); // why it coudnt print that??? return 0; }
output is :
before malloc pointer is :0x6edff4 before malloc valu is :7265660 after malloc pointer is 0x9a52008 after malloc valu is :0 after assignment valu is : 32 after free 0x9a52008 after fee is 0
after free still pointer holds the address of that memory then why we can not print that memory's value.??
what does free() do.?
does it just make all memory as 0 ..??
解决方案after free still pointer holds the address of that memory then why we can not print that memory's value.??
Because the memory no longer belongs to you. You
free
d it, which means the OS is allowed to reuse it however it sees fit, wherever it needs to allocate more memory. You no longer own it, therefore you no longer have any business looking at the value of the data held by that memory.Note also that:
int *ptr; ... printf("Before malloc valu is :%d\n", *ptr);
is equally invalid.
ptr
holds a garbage value, and can point anywhere. Dereferencing it is not guaranteed to be a memory location you can access.Both of these cases invoke undefined behavior, which means the standard says, "DON'T DO THIS," and if you ignore the standard your code will break in horrible ways whenever your boss is looking.
what does free() do.?
does it just make all memory as 0 ..??
No, not necessarily. The OS often zeroes out unused memory in the background to make calls to
calloc
faster, butfree
only tells the operating system "I'm done with this memory, do whatever you need to with it." The OS typically updates some housekeeping data to indicate that the block of memory is no longer owned by a process, so that a later call tomalloc
can use it if it's needed.这篇关于如何免费()的作品?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!