Skip to Content
Navigation:

A stick figure smiling

Here's a preview from my zine, Bite Size Linux!! If you want to see more comics like this, sign up for my saturday comics newsletter or browse more comics!

Image of a comic. To read the full HTML alt text, click "read the transcript".

browse more comics! get the zine!
read the transcript!

your program has memory

10MB: program binary
3MB: stack
587 MB: heap

the heap is what your allocator manages

Your memory allocator (malloc) is responsible for 2 things.

THING 1: keep track of what memory is used/free.

THING 2: Ask the OS for more memory!

malloc: oh no! I’m being asked for 40 MB and I don’t have it.
malloc: can I have 60 MB more?
OS: here you go!

your memory allocator’s interface

  • malloc(size_t size): allocate size bytes of memory & return a pointer to it.
  • free (void* pointer): mark the memory as unused (and maybe give back to the OS)
  • realloc(void pointer, size_t size): ask for more/less memory for pointer.
  • Calloc (size-t members, size_t size): allocate array + initialize to 0.

malloc tries to fill in for space memory when you ask

your code: can I have 512 bytes of memory?
malloc: YES!

malloc isn’t magic! it’s just a function!

you can always: - use a different malloc library like jemalloc or tcmalloc (easy!) - implement your own malloc (harder)

Saturday Morning Comics!

Want another comic like this in your email every Saturday? Sign up here!

I'll send you one of my favourite comics from my archives every Saturday.
© Julia Evans 2024 | All rights reserved (see the FAQ for notes about licensing)