Next: , Previous: , Up: Memory Allocation   [Contents][Index]

3.2.5 Replacing malloc

The GNU C Library supports replacing the built-in malloc implementation with a different allocator with the same interface. For dynamically linked programs, this happens through ELF symbol interposition, either using shared object dependencies or LD_PRELOAD. For static linking, the malloc replacement library must be linked in before linking against libc.a (explicitly or implicitly).

Note: Failure to provide a complete set of replacement functions (that is, all the functions used by the application, the GNU C Library, and other linked-in libraries) can lead to static linking failures, and, at run time, to heap corruption and application crashes.

The minimum set of functions which has to be provided by a custom malloc is given in the table below.


These malloc-related functions are required for the GNU C Library to work.1

The malloc implementation in the GNU C Library provides additional functionality not used by the library itself, but which is often used by other system libraries and applications. A general-purpose replacement malloc implementation should provide definitions of these functions, too. Their names are listed in the following table.


In addition, very old applications may use the obsolete cfree function.

Further malloc-related functions such as mallopt or mallinfo will not have any effect or return incorrect statistics when a replacement malloc is in use. However, failure to replace these functions typically does not result in crashes or other incorrect application behavior, but may result in static linking failures.



Versions of the GNU C Library before 2.25 required that a custom malloc defines __libc_memalign (with the same interface as the memalign function).

Next: , Previous: , Up: Memory Allocation   [Contents][Index]