[plt-scheme] What happens if out of memory

From: Eli Barzilay (eli at barzilay.org)
Date: Sun Jun 12 01:06:22 EDT 2005

On Jun 12, Chihiro Kuraya wrote:
> 
> Eli Barzilay <eli at barzilay.org> wrote:
> > The InsideMz manual says:
> > 
> >   void *scheme_malloc_fail_ok(void *(*mallocf)(size_t size), size_t size)
> > 
> >   Attempts to allocate size bytes using mallocf. If the allocation
> >   fails, the exn:misc:out-of-memory exception is raised.
> 
> Yes, I read this.
> 
> But the thing I want to know is what will happen or 
> what value will be returned, if allocation function such as 
> scheme_malloc fails if I DO NOT wrap the function 
> with scheme_malloc_fail_ok.
> 
> NULL will be returned ?

Well, it's easy to try it out -- with 'fail-ok:

| Welcome to MzScheme version 299.106, Copyright (c) 2004-2005 PLT Scheme, Inc.
| > (require 'foreign)
| > (define a '())
| > (set! a (cons (malloc 1000000000 'fail-ok) a))
| > (set! a (cons (malloc 1000000000 'fail-ok) a))
| > (set! a (cons (malloc 1000000000 'fail-ok) a))
| out of memory 

... so no value is returned, there is just an exception.  Without it:

| Welcome to MzScheme version 299.106, Copyright (c) 2004-2005 PLT Scheme, Inc.
| > (require 'foreign)
| > (define a '())
| > (set! a (cons (malloc 1000000000) a))
| > (set! a (cons (malloc 1000000000) a))
| > (set! a (cons (malloc 1000000000) a))
| GC Warning: Out of Memory!  Returning NIL!
| > a
| (#f #<cpointer> #<cpointer>)

... malloc barfs and you get NULL.

-- 
          ((lambda (x) (x x)) (lambda (x) (x x)))          Eli Barzilay:
                  http://www.barzilay.org/                 Maze is Life!



Posted on the users mailing list.