【发布时间】:2019-09-30 05:12:18
【问题描述】:
如果我使用malloc() 分配内存而不使用free() 取消分配
记忆。为什么其他程序不能通过重写以前的内容来访问该内存
void main()
{
int *a; //here a is holding a junk value
MALLOC(a,1,int);
*a=100; //a is holding 100
MALLOC(a,1,int);
*a=200; //a is holding 200
/* so memory location where the value 100 is stored is inaccessible
cannot be reused */
//why can't we just over write that memory space when used next time
}
【问题讨论】:
-
从来没有那样使用
malloc(我认为标准语法是*malloc(size_t size)。那样你就不能告诉程序在完全相同的位置分配内存。 -
如果你不使用
free(),你已经丢失了第一个malloc分配的内存,你无法访问。你基本上是在泄漏内存。也可以duplicate -
MALLOC是做什么的?
标签: c free dynamic-memory-allocation