A89: Re: when malloc fails...


[Prev][Next][Index][Thread]

A89: Re: when malloc fails...




Hi!

| Consider the following scenario:
| In a C program, some function attempts to malloc() memory and fails due to
| the lack of available RAM.

Yes, on a calculator, this can happen. :-)

| Apparently, the program must exit().

Not necessarily.  You could, for example, break out of a loop, or a
function, or something like that, and thereby exit the program without
explicitly calling exit() or anything similar.

| But If I do so, what happens to the memory that was previously
| allocated on the calling level?

What, exactly, does "on the calling level" mean in this case?

| Is it leaked?

In nostub programs, yes.  Doors frees all memory automatically since some
version.

| If it is, do I handle a situation like this?

As stated above, instead of breaking out of the whole program, break out of
a loop or function, so you can still run your finalization code (freeing all
allocated memory) at the end of the loop or in the calling function.  If you
are in a function, you should probably return an error code, so that the
calling function exits as well (with an error code), and so on, until you
end up at the command line.

Bye,
Sebastian Reichelt




References: