What is Malloc?

15213
Fall 24

A dynamic memory allocator maintains the heap which begins after the uninitialized data area and grows upward. The kernel maintains a variable brk (pronounced “break”) that points to the very top of the heap.

The C standard library provides an explicit allocator known as the malloc package. Programs allocate blocks from the heap by calling the malloc function.

        #include
    void *malloc(size_t size);

Why Dynamic Memory Allocation?

The most important reason that programs use dynamic memory allocation is because they do not know the sizes of certain data structures until the program actually runs.

Constraints

  • Handling arbitrary request sequences. An application can make as many allocate and free requests, and each free request must correspond to a correctly allocated block. The allocator cannot make any assumptions about the ordering of allocate and free requests.
  • Making immediate responses to requests. The allocator is not allowed to reorder or buffer requests in order to improve performance.
  • Uses only the heap. This way, the allocator stays scalable.
  • Aligns blocks. The allocator must align blocks in such a way that they can hold any type of data object, typically multiples of 8, 16, bytes etc.
  • Does not modify allocated blocks. Allocators can only manipulate or change free blocks.

Interact with Cursor