Memory

malloc

  • malloc(size)

  • C89.

  • Allocates size  bytes and returns a pointer to the allocated memory.   The memory is not initialized .  If size  is 0, then malloc()  returns a unique pointer value that can later be successfully passed to free() .

  • For glibc:

    • By default malloc  uses the program break ( sbrk / brk ) for ordinary allocations and switches to mmap  for large allocations.

    • There is a tunable MMAP_THRESHOLD  (initially ~128KB) that controls when mmap  is used for an allocation; this can change dynamically and is adjustable with mallopt . Aallocations below that threshold normally do not  trigger mmap .

  • malloc - Linux Manual .

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 int  if 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 mmap  if 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 mmap  a separate region dedicated to this allocation.

Other Allocators

calloc
  • calloc(count, size)

  • C89.

  • Allocates space for count  elements each of size  bytes and zero-initializes  the entire block.

  • The calloc()  function allocates memory for an array of n  elements of size bytes each and returns a pointer to the allocated memory. The memory is set to zero.  If n  or size is 0 , then calloc()  returns a unique pointer value that can later be successfully passed to free() .

  • If the multiplication of n  and size  would result in integer overflow, then calloc()  returns an error.  By contrast, an integer overflow would not be detected in the following call to malloc() , 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 by p  to size  bytes.  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 p  is NULL , then the call is equivalent to malloc(size) , for all values of size .

  • If size  is equal to zero, and p  is not NULL, then the call is equivalent to free(p) .

  • Unless p  is NULL , it must have been returned by an earlier call to malloc  or related functions.  If the area pointed to was moved, a free(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 mmap  syscalls.

  • 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 memset  function copies the value of (unsigned char)c  into each of the first n  bytes of the object pointed to by s .”

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 memset  implementations almost never look like that—they are highly optimized in assembly, e.g.:

    • glibc (x86)  uses rep stosb  and 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 n  bytes from src  to dest . 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 n  bytes, 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);