Is malloc reentrant

henry at zoo.toronto.edu henry at zoo.toronto.edu
Sun Jul 1 14:32:14 AEST 1990


>Is malloc guaranteed to be reentrant, if not is it reentrant on any
>systems?

Nothing in the C library is guaranteed reentrant by any
manufacturer-independent specification, e.g. ANSI C.  I would be very
surprised to see it reentrant on any implementation that hadn't been
written with something like user-level threads in mind (and suitable tools
on hand to aid in writing it).  Malloc inherently relies on global data
structures to remember the state of memory, and nothing that relies on
global variables is reentrant in general.

In the absence of threads or other such primitives for parallelism, the
only way you'd get malloc re-entered would be from a signal handler.
BEWARE:  there is almost nothing you can do safely and portably in a
signal handler.  ANSI C guarantees that you can (a) call signal() for the
same signal, (b) assign a value to a static variable of one very specific
type, and (c) *nothing else*.  1003.1 makes some somewhat stronger
promises, but the ability to call malloc() is not among them.

                                         Henry Spencer at U of Toronto Zoology
                                          henry at zoo.toronto.edu   utzoo!henry



More information about the Comp.sys.sun mailing list