tags:

views:

841

answers:

7

Recently, I work on a video player program on Windows for a CCTV program. As the program has to decode and play many videos streams at the same time, I think it might meet the situation that malloc will fail and I add checking after every malloc.

But genrally speaking, in these code of open source programs that I've read in open source projects, I seldom find any checking of result of malloc. So when malloc fails, most program will just crash. Isn't that unacceptalbe?

My colleagues who write server programs on linux will alloc a enough memory for 100 client connections. So although his program might refuse the 101 client, it will never met a failure of malloc. Is his approach also suitable for desktop applications?

+1  A: 

To use the result of malloc without checking for null is unacceptable in code that might be open to use on platforms where malloc can fail, on those it will tend to result in crashes and unpredicatable behaviour. I can't forsee the future, don't know where my code will go, so I would write code with checks for malloc returning null - better to die than behave unpredicatbly!

Strategies for what to do if malloc fails depend upon the kind of applciation and how much confidence you have in the libraries you are using. It some situations the only safe thing to do is halt the whole program.

The idea of preallocating a known quota of memory and parcelling out in some chunks, hence steering clear of actually exhausting the memeory is a good one, if your application's memory usage is predicatable. You can extend this to writing your own memory management routines for use by your code.

djna
+11  A: 

On Linux, malloc() will never fail -- instead, the OOM killer will be triggered and begin killing random processes until the system falls over. Since Linux is the most popular UNIX derivative in use today, many developers have learned to just never check the result of malloc(). That's probably why your colleagues ignore malloc() failures.

On OSes which support failures, I've seen two general patterns:

  • Write a custom procedure which checks the result of malloc(), and calls abort() if allocation failed. For example, the GLib and GTK+ libraries use this approach.

  • Store a global list of "purge-able" allocations, such as caches, which can be cleared in the event of allocation failure. Then, try the allocation again, and if it still fails, report it via the standard error reporting mechanisms (which do not perform dynamic allocation).

John Millikin
The practice of not checking the return value of malloc has been going on a lot longer than Linux has been around. It's related to the mentality of not checking the return value of printf -- what are the chances that either of them will fail? But the man page for malloc lists the OOM behavior as a "bug" and describes a way to disable it so malloc returns NULL in case of failure.
Barry Brown
@Barry: I don't check the return value of `printf()`, not because I think it's infallible, but because I don't care whether or not it fails.
John Millikin
When you say it cannot fail, you really mean that it does not fail gracefully in a way that the program can manage or control. See this discussion http://ubuntuforums.org/archive/index.php/t-1214975.html
Clifford
@John Millikin: you don't check the return value from printf() because it does not return an error status, it merely returns the number of characters output. So that argument is false.
Clifford
@Clifford: if the number of characters output is less than the number I was expecting it to write, then it failed.
John Millikin
To be exact, it can fail but it never returns NULL.
Daniel Earwicker
It is easy to provoke `malloc` to return NULL on a Linux system - just set a process memory limit with `ulimit` before you run your program. This is useful if you want to test how your program handles the out of memory case.
caf
+4  A: 

Follow the standardized API

Even on Linux, ulimit can be used to get a prompt malloc error return. It's just that it defaults to unlimited.

There is a definite pressure to conform to published standards. On most systems, in the long run, and eventually even on Linux, malloc(3) will return a correct indication of failure. It is true that desktop systems have virtual memory and demand paging, but even then not checking malloc(3) only works in a debugged program with no memory leaks. If anything goes wrong, someone will want to set a ulimit and track it down. Suddenly, the malloc check makes sense.

DigitalRoss
A: 

Always check, and pre-allocate a buffer that can be freed in this case so you can warn the user to save his data and shut down the application.

Larry Watanabe
A: 

It depends on the type of application that you are working on. If the application does work that is divided into discreet tasks where an individual task can be allowed to fail, then checking memory allocations can be recovered from gracefully.

But in many cases, the only reasonable way to respond to a malloc failure is by terminating the program. Allowing your code to just crash on the inevitable null dereference will achieve that. It would certainly always be better to dump a log entry or error message explaining the error, but in the real world we work on limited schedules. Sometimes the return on investment of pedantic error handling isn't there.

Alan
A: 

Depends on the app you write. Of course you always need to check the return value of malloc(). However, handling OOM gracefully only makes sense in very cases, such as low-level crucial system services, or when writing a library that might be used be them. Having a malloc wrapper that aborts on OOM is hence very common in many apps and frameworks. Often those wrappers are named xmalloc() or similar.

GLib's g_malloc() is aborting, too.

A: 

If you are going to handle huge amounts of memory, and want to make statements to Linux like "now I have memory area ABC and I don't need the B piece, do as you wish", have a look to mmap()/madvise() family of functions available in stock GNU C library. Depending on your usage patterns, the code can end up even simpler than using malloc. This API can also be used to help Linux not waste memory by caching files you are going to read/write only once.

They are nicely documented in GNU libc info documentation.

Juraj