blank allocation

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
Ozguxxx

blank allocation

Post by Ozguxxx »

My question is simple. Suppose we only allocate memory but we do not get pointer returned to it. What happens? Is there a way to free this memory? I think Java Garbage collectors do something like this but I dont know how to realize it. (In fact I did not think about it for long time.)
I mean:

for(200 times)
{
malloc(100);
}

this loop allocates 20K, but is there a way to free it?
Tim

Re:blank allocation

Post by Tim »

Not if you're using standard malloc/free. If you're allocating memory, why aren't you saving a pointer to it?

You could start implementing a garbage collector (C doesn't give you one), but then you have to answer the questions: when is memory no longer needed? And where is it located?
Ozguxxx

Re:blank allocation

Post by Ozguxxx »

I thought of a person who wants to crash computer.
I have currently about to finish malloc implementation (I think) and suddenly I was shocked by this fact I mean I thought when one allocates virtual memory, this memory is recorded by vmm but if a person does not assign a pointer to returned value vmm might (I do not know how) see this and automatically free memory itself.
Tim

Re:blank allocation

Post by Tim »

There's no reason why the kernel can't free the memory when the process exits. Remember, malloc() gets it memory in large blocks from the VMM; the VMM knows where these large blocks are located and how big they are, and, since the VMM is part of the kernel, it can free the memory when the process exits. You could also impose limits ('quotas') on the amount of memory a process can allocate, in order to stop it hogging everything.

However, since you're probably using paging, you can allocate as much virtual address space as you want without allocating physical storage until that memory is actually used.
Post Reply