malloc out of memory

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

User avatar
lucasart
Posts: 3232
Joined: Mon May 31, 2010 1:29 pm
Full name: lucasart

malloc out of memory

Post by lucasart »

what is the correct way (in C not C++) to handle a failed memory allocation ? i was neively hoping that the result of a failed malloc would be NULL, but it seems to be somewhat more complicated. Should I test errno == ENOMEM and free the pointer before quitting the program ?
User avatar
sje
Posts: 4675
Joined: Mon Mar 13, 2006 7:43 pm

Re: malloc out of memory

Post by sje »

Try the man page "man malloc".

Code: Select all

RETURN VALUES
     If successful, calloc(), malloc(), realloc(), reallocf(), and valloc() functions return a
     pointer to allocated memory.  If there is an error, they return a NULL pointer and set
     errno to ENOMEM.

     For realloc(), the input pointer is still valid if reallocation failed.  For reallocf(),
     the input pointer will have been freed if reallocation failed.

     The free() function does not return a value.
User avatar
lucasart
Posts: 3232
Joined: Mon May 31, 2010 1:29 pm
Full name: lucasart

Re: malloc out of memory

Post by lucasart »

thanks!

I read somewhere else that a failed malloc would return a pointer to whatever has been allocated. But that must have been some noob writing in a forum not knowing what he's talking about ;-)

so it's very simple, just check the errno and that's it.

PS: I wouldn't have guessed that there would be a man page for malloc(). After all it's not a shelll command but a C function. Although the history of C and UNIX is so intertwined...
User avatar
lucasart
Posts: 3232
Joined: Mon May 31, 2010 1:29 pm
Full name: lucasart

Re: malloc out of memory

Post by lucasart »

by the way I'm also using realloc when I want to resize my hash table. And the main reason is that I want to preserve the current hash. reallocf is just perfect.
User avatar
lucasart
Posts: 3232
Joined: Mon May 31, 2010 1:29 pm
Full name: lucasart

Re: malloc out of memory

Post by lucasart »

Unfortunately it seems that all the precautions are useless, because the same man page states that:

Code: Select all

BUGS
       By default, Linux follows an  optimistic  memory  allocation  strategy.
       This  means  that  when malloc() returns non-NULL there is no guarantee
       that the memory really is available.  This is a  really  bad  bug.   In
       case  it  turns  out that the system is out of memory, one or more pro‐
       cesses will be killed by the infamous OOM killer.   In  case  Linux  is
       employed  under  circumstances where it would be less desirable to sud‐
       denly lose some randomly picked processes, and moreover the kernel ver‐
       sion  is  sufficiently  recent,  one can switch off this overcommitting
       behavior using a command like:

           # echo 2 > /proc/sys/vm/overcommit_memory
:(
User avatar
lucasart
Posts: 3232
Joined: Mon May 31, 2010 1:29 pm
Full name: lucasart

Re: malloc out of memory

Post by lucasart »

ever heard of the "infamous OOM killer" ? This could be a title for a horror movie :evil:
User avatar
lucasart
Posts: 3232
Joined: Mon May 31, 2010 1:29 pm
Full name: lucasart

Re: malloc out of memory

Post by lucasart »

lucasart wrote:ever heard of the "infamous OOM killer" ? This could be a title for a horror movie :evil:
OK if even the kernel Linux is not capable of handling an out of memory correctly, then I'm not going to bother doing it in my program. For all I know, a failed malloc might get my process killed by the "infamous OOM killer".
User avatar
lucasart
Posts: 3232
Joined: Mon May 31, 2010 1:29 pm
Full name: lucasart

Re: malloc out of memory

Post by lucasart »

actually I read some more about the infamous OOM killer, and it only gets triggered when there's no more RAM, *and* all the swap space (virtual memory on disk) also runs out. So this should not happen in practice. Besides, when that situation happens on Windows... There's no OOM killer to save you and your system just becomes unusable, and there's nothing you can do. Those who have experienced a disk full on Windows will know what I'm talking about...
User avatar
sje
Posts: 4675
Joined: Mon Mar 13, 2006 7:43 pm

Re: malloc out of memory

Post by sje »

The default behavior in C++ and Pascal is to crash when new() asks for more space than is available. And on modern desktop/notebook hardware, this is a reasonable outcome as asking for so much space is most certainly to be a programming error.

My program Symbolic queries the OS at start-up time to determine the amount of physical RAM and uses the result to ensure that new() calls will not fail and in fact will not use more RAM than is available. This allows Symbolic to optimally use RAM on machines ranging from my 2002 iBook (640 MB RAM) to my 2006 Mac Pro (24 GB RAM).
User avatar
sje
Posts: 4675
Joined: Mon Mar 13, 2006 7:43 pm

Determining RAM availability

Post by sje »

Here is Symbolic's code for determining physical RAM availability for OpenBSD (Apple Mac OS/X) and for Linux:

Code: Select all

typedef unsigned int ui;
typedef unsigned long long ui64;

ui64 PhysicalBytes(void)
{
  ui64 count;

#if HostOsApple
  {
    int mib[2];
    size_t countlen = sizeof(count);

    mib[0] = CTL_HW;
    mib[1] = HW_MEMSIZE;
    sysctl(mib, 2, &count, &countlen, NULL, 0);
  };
#endif

#if HostOsLinux
  {
    const ui pagecount = (ui) sysconf(_SC_PHYS_PAGES);
    const ui pagesize = (ui) sysconf(_SC_PAGE_SIZE);

    count = ((ui64) pagecount) * ((ui64) pagesize);
  };
#endif

  return count;
}