This article was accepted into the corpus but its outbound wikilinks were never NER-processed — typical at the deepest BFS hop or when the run's entity cap was reached. No expansion funnel to show.
| malloc | |
|---|---|
| Name | malloc |
| Type | C library function |
| Developer | Dennis Ritchie, Ken Thompson, Bell Labs |
| First appeared | 1970s |
| Operating system | Unix, Linux, macOS, Windows NT |
| License | ISC license; implementation-dependent |
malloc
malloc is a C library function providing dynamic memory allocation at runtime. It enables programs written in C, C++, and other languages with C interoperability to request uninitialized memory from the heap, facilitating data structures such as linked lists, trees, and buffers used in systems like UNIX and Linux. Originating in the early systems programming era at Bell Labs, malloc has become a cornerstone API in many runtime environments, standard libraries, and operating system memory subsystems.
malloc serves as an allocator interface between user programs and the underlying kernel memory facilities such as virtual memory and sbrk/mmap on Unix-like systems. It returns a pointer to a contiguous block of memory or a null pointer on failure, allowing programs to grow and shrink storage needs dynamically in response to data demands encountered in applications like Apache HTTP Server, PostgreSQL, Nginx, and Redis. Multiple implementations and tuning options exist across ecosystems including glibc, musl, jemalloc, tcmalloc, and platform-specific variants on Windows NT and macOS.
The standard prototype for malloc appears in the ISO/IEC 9899 C standard and is declared in
Allocator implementations employ a variety of strategies including splitting and coalescing free blocks, segregated free lists, and buddy allocators. Notable algorithms appear in research and implementations associated with AT&T Bell Laboratories and later in projects such as Mozilla-backed jemalloc and Google’s tcmalloc. Techniques like first-fit, best-fit, next-fit, and segregated fits trade off fragmentation, throughput, and latency in servers like Nginx and databases like MySQL. Low-level integration with virtual memory mechanisms—using mmap for large allocations and sbrk for heap growth—affects interaction with system components including Linux kernel memory management and macOS memory APIs.
malloc-managed memory is placed on the heap and subject to allocator bookkeeping that can cause internal and external fragmentation; these behaviors influence long-running services such as Apache HTTP Server, PostgreSQL, and Redis. The lifecycle of allocated memory requires explicit deallocation with free to avoid leaks that tools like Valgrind and AddressSanitizer detect in projects like Chromium and Firefox. Interactions with thread-local caches, memory arenas, and operating system page allocation strategies impact behavior in multithreaded applications such as OpenSSL-based servers and NGINX worker processes.
The behavior and signature of malloc are specified by the ISO/IEC 9899 standard (C99, C11, C18) and further constrained by POSIX for UNIX-like systems; implementations in glibc, musl, and BSD-derived libraries aim to comply while offering extensions. Differences arise across platforms—Windows NT provides HeapAlloc and VirtualAlloc semantics, while macOS employs libsystem variants and Mach VM primitives—impacting portability of low-level assumptions in cross-platform projects like Qt, LLVM, and Mono.
Allocator performance affects throughput and latency in high-performance systems such as Hadoop, Kafka, Redis, and MySQL. Optimizations include thread-local caches, lock-free arenas, batching of metadata updates, and size-class-specific fast paths implemented in jemalloc and tcmalloc. Profiling tools from Google and Microsoft and integration with performance suites like SPEC CPU guide tuning decisions. Application-level strategies—object pooling, slab allocation as in Linux slab allocator concepts, and custom allocators used in Boost libraries—mitigate allocation overhead and fragmentation for latency-sensitive services like nginx and HAProxy.
Allocator bugs and misuse have led to vulnerabilities exploited in software such as Mozilla Firefox, Chromium, and various server stacks. Common issues include heap-based buffer overflows, use-after-free, double-free, and heap metadata corruption; mitigations include hardened allocators, safe unlinking checks, and runtime mitigations like Address Space Layout Randomization and Data Execution Prevention. Tools such as Valgrind, AddressSanitizer, and MemorySanitizer aid detection in projects backed by organizations like Google and Mozilla, while secure coding standards from bodies like CERT recommend patterns to reduce exposure in critical infrastructure such as OpenSSL and LibreSSL.
Category:C (programming language) functions