OSTEP Ch. 14: malloc, free and 7 Ways to Break Memory
James Software Tips
0:00 / 0:00
OSTEP Ch. 14: malloc, free and 7 Ways to Break Memory
1 просмотр · 6 ч назад
James Software Tips
51 подписчик
1 просмотр · 6 ч назад
I read OSTEP Chapter 14, the Memory API interlude: stack vs heap, malloc() and free(), why sizeof can fool you, the seven classic memory bugs (segfaults, buffer overflows, leaks, dangling pointers, double frees...), and what malloc is really built on: brk, sbrk and mmap.
Operating Systems: Three Easy Pieces (free online): https://pages.cs.wisc.edu/~remzi/OSTEP/
This chapter (PDF): https://pages.cs.wisc.edu/~remzi/OSTE...
Chapters
0:00 Chapter 14 recap
1:00 Intro
2:33 14.1 Types of memory
5:07 14.2 The malloc() call
8:33 sizeof: pointer vs array
9:46 14.3 The free() call
10:18 14.4 Common errors
15:24 What Rust actually fixes
16:55 Two levels of memory management
18:46 Tools: valgrind and AddressSanitizer
18:57 14.5 Underlying OS support
20:16 14.6 Other calls: calloc and realloc
20:50 14.7 Summary
Quick notes
sizeof is compile-time. On a pointer it gives the pointer size (8 on 64-bit), on an array the whole array.
Strings need malloc(strlen(s) + 1) for the terminating zero byte.
free() takes only the pointer: the allocator remembers the size.
Seven ways to get it wrong: forget to allocate, allocate too little, read uninitialized memory, forget to free, free too early, free twice, free something malloc never gave you.
malloc and free are library calls. Underneath, the C library asks the kernel for memory with brk/sbrk and mmap.
On a Mac: valgrind runs in a Linux VM (Docker Desktop, OrbStack, Lima); natively use clang -fsanitize=address or leaks.
#operatingsystems #ostep #cprogramming #memorymanagement #computerscience