malloc
-
malloc(size) -
C89.
-
Allocates
sizebytes and returns a pointer to the allocated memory. The memory is not initialized . Ifsizeis 0, thenmalloc()returns a unique pointer value that can later be successfully passed tofree(). -
For glibc:
-
By default
mallocuses the program break (sbrk/brk) for ordinary allocations and switches tommapfor large allocations. -
There is a tunable
MMAP_THRESHOLD(initially ~128KB) that controls whenmmapis used for an allocation; this can change dynamically and is adjustable withmallopt. Aallocations below that threshold normally do not triggermmap.
-
Example
-
malloc(sizeof(int))(4 bytes)-
rounded up to alignment/size-class → typically 8 or 16 bytes.
-
allocator finds a free block in the 8B/16B size-class run and returns a pointer to the payload.
-
metadata: either a small header immediately before the payload (ptmalloc-style) or a bitmap/side-table recording that this slot is used (jemalloc-style).
-
-
malloc(sizeof(uint64_t))(8 bytes)-
rounded to same 8B/16B class as above; may sit next to the
intif the allocator gave contiguous small blocks. -
allocation cost is O(1) if a free block exists in that class.
-
-
malloc(100)(string/array)-
rounded up to the nearest size class (e.g. 112 or 128 bytes depending on class table).
-
allocator picks a block from the corresponding run; if the run has no free blocks it may split a larger free block or request more runs/pages from the chunk (or request a new chunk via
mmapif the chunk is exhausted).
-
-
malloc(1 << 20)(1 MiB — large example)-
this may be larger than the allocator’s “small” classes and will likely be satisfied by allocating a span of pages (one or more pages) from the chunk; if it’s larger than allocator thresholds, the allocator may
mmapa separate region dedicated to this allocation.
-
Other Allocators
calloc
-
calloc(count, size) -
C89.
-
Allocates space for
countelements each ofsizebytes and zero-initializes the entire block. -
The
calloc()function allocates memory for an array ofnelements of size bytes each and returns a pointer to the allocated memory. The memory is set to zero. Ifnor size is0, thencalloc()returns a unique pointer value that can later be successfully passed tofree(). -
If the multiplication of
nandsizewould result in integer overflow, thencalloc()returns an error. By contrast, an integer overflow would not be detected in the following call tomalloc(), with the result that an incorrectly sized block of memory would be allocated:
realloc
-
realloc() -
The
realloc()function changes the size of the memory block pointed to byptosizebytes. The contents of the memory will be unchanged in the range from the start of the region up to the minimum of the old and new sizes. If the new size is larger than the old size, the added memory will not be initialized. -
If
pisNULL, then the call is equivalent tomalloc(size), for all values ofsize. -
If
sizeis equal to zero, andpis not NULL, then the call is equivalent tofree(p). -
Unless
pisNULL, it must have been returned by an earlier call tomallocor related functions. If the area pointed to was moved, afree(p)is done.
jemmalloc
-
jemmaloc -
Typically obtain large fixed-size chunks from the OS (4 MiB is a common default chunk size on 64-bit builds) and then carve many small allocations out of those chunks. That’s why a bunch of 1-byte allocations won’t generate many
mmapsyscalls. -
Focuses on low fragmentation, predictable behavior, per-arena concurrency, and strong profiling/monitoring features. Widely used in servers and language runtimes.
tcmalloc
-
tcmalloc(Google) -
Thread-caching allocator optimized for low-lock fast allocation on many threads (per-thread caches + central free lists). Good throughput in highly concurrent workloads.
mimalloc
-
mimalloc(Microsoft) -
Compact, high-performance general-purpose allocator that emphasizes speed and low overhead; simple drop-in use and some novel design choices for cross-thread frees.
Mem Set
-
Example .
-
“The
memsetfunction copies the value of(unsigned char)cinto each of the firstnbytes of the object pointed to bys.”
void *memset(void *s, int c, size_t n) {
unsigned char *p = s;
unsigned char value = (unsigned char)c;
for (size_t i = 0; i < n; i++) {
p[i] = value;
}
return s;
}
-
This snippet is effectively what you’d write to satisfy the standard in the simplest way possible. It’s not from glibc, musl, BSD, or any other real system library.
-
Real-world
memsetimplementations almost never look like that—they are highly optimized in assembly, e.g.:-
glibc (x86) uses
rep stosband alignment tricks. -
musl uses word-sized writes in tight loops, with SIMD paths.
-
newlib and BSD libc have similar optimizations tuned per architecture.
-
Mem Copy
memcopy
-
memcpy(void *dest, const void *src, size_t n) -
Copies
nbytes fromsrctodest. Behavior is undefined if source and destination overlap. Typically faster because it assumes no overlap and may use wider loads/stores or hardware instructions.
memmove
-
memmove(void *dest, const void *src, size_t n) -
Copies
nbytes, but guarantees correctness when source and destination overlap. It does this by checking pointer order and copying in the correct direction (forward or backward) to prevent overwriting unread data. -
In summary, same thing that
memcopy, but handles overlap.
Mem Zero
-
void bzero(void *s, size_t n); -
Is a legacy C library function that sets a block of memory to zero.
-
Equivalent to:
-
memset(s, 0, n);