When to check the return value of malloc



Howdy,

I was reflecting recently on malloc.

Obviously, for tiny allocations like 20 bytes to strcpy a filename or
something, there's no point putting in a check on the return value of
malloc.

OTOH, if you're allocating a gigabyte for a large array, this might
fail, so you should definitely check for a NULL return.

So somewhere in between these extremes, there must be a point where you
stop ignoring malloc's return value, and start checking it.

Where do people draw this line? I guess it depends on the likely system
the program will be deployed on, but are there any good rule-of-thumbs?

Rgds,
MJ

.



Relevant Pages

  • Re: When to check the return value of malloc
    ... I was reflecting recently on malloc. ... for tiny allocations like 20 bytes to strcpy a filename or ...
    (comp.lang.c)
  • Re: xmalloc string functions
    ... than a NULL return from malloc(). ... pointer value to null at the point I want to trigger the failure. ... There are about five bazillion allocations, ... Memory is quite a different kind of resource. ...
    (comp.lang.c)
  • Re: Compiler Qusetion, -taso flag
    ... __sbrk_override malloc tuning variable, or to set the ... A good description of the taso programming environment is contained ... In a program linked with -taso and with shared libraries, ... The most effective solution for large allocations in taso applications ...
    (comp.unix.tru64)
  • Re: OT: Memory profiling on Linux
    ... I need to profile my milter daemon on linux. ... Then you can go through the allocation log, and any allocations that don't ... #define MALLOC malloc ... void *mymalloc ...
    (comp.lang.c)
  • Re: When to check the return value of malloc
    ... If you are caling malloc() then usually it means that the state is currently in a corrupt or partially constructed situation. ... program's data structures, ... Nemployees is needed to destroy the names correctly, but salaries is null. ... We've got to test for several patterns of allocations failing and the recovery code saving the state properly and destroying the object properly. ...
    (comp.lang.c)