diff options
author | hyang <hyang@hyang.xyz> | 2023-12-21 20:28:20 -0800 |
---|---|---|
committer | hyang <hyang@hyang.xyz> | 2023-12-21 20:30:11 -0800 |
commit | 36156fa9a4a9a82faabb19b30c72ca262adf5e7f (patch) | |
tree | 294d817d8085d73f3ee3dee53d6e93137b6a5b9e /content | |
parent | f2e82cd2a7ac77363d57f96cd807f92912837f48 (diff) |
xd
Diffstat (limited to 'content')
-rw-r--r-- | content/posts/malloc/images/memory.png | bin | 0 -> 48327 bytes | |||
-rw-r--r-- | content/posts/malloc/index.md | 18 |
2 files changed, 18 insertions, 0 deletions
diff --git a/content/posts/malloc/images/memory.png b/content/posts/malloc/images/memory.png Binary files differnew file mode 100644 index 0000000..e5d347e --- /dev/null +++ b/content/posts/malloc/images/memory.png diff --git a/content/posts/malloc/index.md b/content/posts/malloc/index.md new file mode 100644 index 0000000..76cbf73 --- /dev/null +++ b/content/posts/malloc/index.md @@ -0,0 +1,18 @@ +--- +title: "Adventures with Malloc" +date: 2023-12-21T14:34:30-08:00 +draft: false +--- + +Recently, I have been cursed with a series of videos on my YouTube feed related to malloc. I've decided to dive deep into the malloc rabbit hole as well. + +For those who don't know, to simply put, `malloc()`, and `free()`, is how C manages dynamic memory (memory that should be handled by the caller). `malloc()` simply allocates n bytes of dynamic memory, and returns a pointer to it. Unused dynamic memory should be freed by the caller with `free()`. You can read more about it [here](https://linux.die.net/man/3/malloc). + +Very simple concept! However, the implementation of malloc is far from simple... + +To start, the heap is managed entirely by `malloc` and `free`. As a result, according to this [Wikipedia page](https://en.wikipedia.org/wiki/C_dynamic_memory_allocation), there are 8 different well-known unique implementations of malloc. A notable one, [mimalloc](https://github.com/microsoft/mimalloc), claiming to the fastest, and small(?), is 8K LOC. For educational purposes, I've decided to to stick to the simplest implementation. + + +## Before the Heap +Program memory is divided into various segments: stack, heap, data, and text. As you + |