🎞️

Malloc Implementation

TypeMaster

Freelist

keep track of available memory which could be all over the heap

A linked list of all available memory block. Specifically, blocks that are NOT currently allocated

Metadata

Malloc and Freelist

Can't find a block of the right size?

Divide the block into two parts, one of the correct size, and one with whatever’s left over

Remove and return new block with proper size

don’t have any blocks that are big enough?

Out variable

int main(void) {
  char *s; // hello will write to this
  int len = hello(&s);
  // now 's' is "hello"
  ...
}
//Caller passes a pointer to a local variable

// Returns a string along with its length
int hello(char **out) {
  char *c = "hello";
  // how do we return 'c' in 'out'?
  return strlen(c);
}
//Callee uses the pointer to modify the caller's local 
variable