On Mon, 24 Mar 2008 22:39:25 -0700, Peter Michaux wrote:
Suppose I have implemented a language with garbage collection in C. I
have wrapped malloc in my own C function. If malloc returns NULL then I
can run the garbage collector and then try malloc again. What do I do if
malloc returns NULL again? Can a C program ask the operating system for
more space and then I could try malloc a third time?
No, or at least, not portably, and I'm not aware of any system-specific
ways. If malloc returns NULL, and you've freed everything that can be
freed, there just isn't enough memory available. One way to handle this
is abort (either the program, or the current operation), and show the
user an error. Another is to pause, asking the user to shut down other
applications. But in the second case, that's not something that the OS
should do automatically.