You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
14 KiB
14 KiB
Learn Memory Management in C
Completed course from boot.dev.
The exercises from the course have been worked out in this repo. This is my way of having the "certificate" for free. The first the 3 chapters of the course can be done interactively on the website.
If you're reading this with the intention of completing the exercises yourself,
you can find each exercise is in its own directory inside of the src
directory.
Remove all the files that are not called main.c
and recreate them from the
exercises on the boot.dev website as you go through them, so you won't get spoiled.
Note:
- In order to verify allocations in the unit tests, please use the wrapper
functions
boot_malloc
,boot_calloc
,boot_realloc
andboot_free
fromboot_lib.h
instead of thestdlib.h
allocation functions - In chapter 6 exercise 6, macOS malloc will almost never return a NULL pointer
due to virtual memory overcommit, so you should check for
size == 1024 * 1024 * 100)
instead ofarray == NULL
- In chapter 8, I renamed
stack_t
tomy_stack_t
due to astd
naming conflict on macOS
Build instructions
Compiling and running all the tests can be done with the following commands:
$ mkdir build
$ cd build
$ cmake ..
$ make run
Output
Successful output of all the tests:
$ make run 12:25PM
[ 3%] Built target munit
[ 6%] Built target 9-8-length
[ 7%] Built target 4-1-enums
[ 9%] Built target 4-2-non-default-values
[ 12%] Built target 4-3-switch-case
[ 14%] Built target 4-4-sizeof-enum
[ 17%] Built target 5-1-union
[ 19%] Built target 5-2-memory-layout
[ 21%] Built target 5-3-5-4-union-size
[ 23%] Built target 5-5-helper-fields
[ 25%] Built target 6-1-the-stack
[ 27%] Built target 6-2-why-a-stack
[ 29%] Built target 6-3-stack-overflow
[ 31%] Built target 6-4-pointers-to-the-stack
[ 34%] Built target 6-5-the-heap
[ 37%] Built target 6-6-malloc
[ 40%] Built target 6-7-free
[ 42%] Built target 6-8-big-endian-little-endian
[ 45%] Built target 7-1-pointer-pointers
[ 48%] Built target 7-2-array-of-pointers
[ 51%] Built target 7-3-void-pointers
[ 54%] Built target 7-4-swapping-integers
[ 57%] Built target 7-5-swapping-strings
[ 60%] Built target 7-6-generic-swap
[ 63%] Built target 8-1-low-level-stack
[ 66%] Built target 8-2-stack-push
[ 69%] Built target 8-3-stack-pop
[ 72%] Built target 8-4-stack-free
[ 76%] Built target 8-5-dangerous-push
[ 80%] Built target 8-6-multiple-types
[ 82%] Built target 9-1-snek-objects
[ 85%] Built target 9-2-integer
[ 88%] Built target 9-3-float
[ 91%] Built target 9-4-string
[ 94%] Built target 9-5-vector3
[ 97%] Built target 9-6-arrays
[100%] Built target 9-7-get-and-set
Running test suite with seed 0xa13b83c4...
colors/are_defined [ OK ] [ 0.00000100 / 0.00000300 CPU ]
colors/are_defined_correctly [ OK ] [ 0.00000100 / 0.00000200 CPU ]
2 of 2 (100%) tests successful, 0 (0%) test skipped.
Running test suite with seed 0x9c11f043...
colors/defined [ OK ] [ 0.00000000 / 0.00000000 CPU ]
colors/defined_vscode [ OK ] [ 0.00000000 / 0.00000000 CPU ]
2 of 2 (100%) tests successful, 0 (0%) test skipped.
Running test suite with seed 0xdac57c79...
http/switch_enum [ OK ] [ 0.00000100 / 0.00000100 CPU ]
http/switch_enum_default [ OK ] [ 0.00000100 / 0.00000100 CPU ]
2 of 2 (100%) tests successful, 0 (0%) test skipped.
The size of BigNumbers is 8 bytes
The size of HttpErrorCode is 4 bytes
Running test suite with seed 0x85251cb1...
format/integer [ OK ] [ 0.00000100 / 0.00000100 CPU ]
format/string [ OK ] [ 0.00000100 / 0.00000100 CPU ]
format/integer_nvim [ OK ] [ 0.00000100 / 0.00000200 CPU ]
format/string_nvim [ OK ] [ 0.00000100 / 0.00000100 CPU ]
4 of 4 (100%) tests successful, 0 (0%) test skipped.
value (set): -420
err (unset): 4294966876
value (unset): -1
err (set): 4294967295
Running test suite with seed 0xb2bd8586...
PacketHeader/test_packet_header_size [ OK ] [ 0.00000100 / 0.00000000 CPU ]
PacketHeader/test_tcp_header_fields [ OK ] [ 0.00000100 / 0.00000100 CPU ]
PacketHeader/test_field_raw_size [ OK ] [ 0.00000100 / 0.00000100 CPU ]
PacketHeader/test_field_to_raw_consistency[ OK ] [ 0.00000100 / 0.00000200 CPU ]
4 of 4 (100%) tests successful, 0 (0%) test skipped.
---------------------------------
Stack pointer offset: 0 bytes
---------------------------------
Dark mode?
---------------------------------
Stack pointer offset: 0 bytes
---------------------------------
More like...
---------------------------------
Stack pointer offset: 0 bytes
---------------------------------
dark roast.
Running test suite with seed 0x5ed24c16...
/example/compare [ OK ] [ 0.00000100 / 0.00000200 CPU ]
/example/rand [ OK ] [ 0.00000200 / 0.00000100 CPU ]
/example/parameters
foo=one, bar=red [ OK ] [ 0.00000200 / 0.00000200 CPU ]
foo=one, bar=green [ OK ] [ 0.00000200 / 0.00000200 CPU ]
foo=one, bar=blue [ OK ] [ 0.00000200 / 0.00000200 CPU ]
foo=two, bar=red [ OK ] [ 0.00000200 / 0.00000100 CPU ]
foo=two, bar=green [ OK ] [ 0.00000200 / 0.00000200 CPU ]
foo=two, bar=blue [ OK ] [ 0.00000200 / 0.00000200 CPU ]
foo=three, bar=red [ OK ] [ 0.00000200 / 0.00000200 CPU ]
foo=three, bar=green [ OK ] [ 0.00000200 / 0.00000200 CPU ]
foo=three, bar=blue [ OK ] [ 0.00000100 / 0.00000000 CPU ]
11 of 11 (100%) tests successful, 0 (0%) test skipped.
Size of pool: 10240
Initial string: snek
c1: 10, 20
c2: 30, 40
c3: 50, 60
Running test suite with seed 0x08ecbbe7...
get_full_greeting/test_basic_greeting[ OK ] [ 0.00000200 / 0.00000200 CPU ]
get_full_greeting/test_short_buffer [ OK ] [ 0.00000100 / 0.00000200 CPU ]
2 of 2 (100%) tests successful, 0 (0%) test skipped.
Running test suite with seed 0xd953a05a...
allocate_scalar_array/test_allocate_scalar_array_size[ OK ] [ 0.00000100 / 0.00000100 CPU ]
allocate_scalar_array/test_allocate_scalar_array_values[ OK ] [ 0.00000200 / 0.00000100 CPU ]
allocate_scalar_array/test_allocate_scalar_array_zero_multiplier[ OK ] [ 0.00000200 / 0.00000200 CPU ]
allocate_scalar_array/test_allocate_too_much[ OK ] [ 0.00001000 / 0.00000900 CPU ]
4 of 4 (100%) tests successful, 0 (0%) test skipped.
Allocated 500 lists
Running test suite with seed 0x026c49ea...
/example/compare [ OK ] [ 0.00000100 / 0.00000200 CPU ]
/example/rand [ OK ] [ 0.00000100 / 0.00000200 CPU ]
/example/parameters
foo=one, bar=red [ OK ] [ 0.00000200 / 0.00000100 CPU ]
foo=one, bar=green [ OK ] [ 0.00000100 / 0.00000100 CPU ]
foo=one, bar=blue [ OK ] [ 0.00000100 / 0.00000200 CPU ]
foo=two, bar=red [ OK ] [ 0.00000100 / 0.00000200 CPU ]
foo=two, bar=green [ OK ] [ 0.00000100 / 0.00000200 CPU ]
foo=two, bar=blue [ OK ] [ 0.00000200 / 0.00000100 CPU ]
foo=three, bar=red [ OK ] [ 0.00000200 / 0.00000100 CPU ]
foo=three, bar=green [ OK ] [ 0.00000200 / 0.00000100 CPU ]
foo=three, bar=blue [ OK ] [ 0.00000200 / 0.00000200 CPU ]
11 of 11 (100%) tests successful, 0 (0%) test skipped.
Running test suite with seed 0xbb38daad...
allocate_list/create [ OK ] [ 0.00000100 / 0.00000200 CPU ]
allocate_list/overwrite [ OK ] [ 0.00000100 / 0.00000000 CPU ]
2 of 2 (100%) tests successful, 0 (0%) test skipped.
Running test suite with seed 0x33807c18...
create_token_pointer_array/test_create_token_pointer_array_single[ OK ] [ 0.00000100 / 0.00000200 CPU ]
create_token_pointer_array/test_create_token_pointer_array_multiple[ OK ] [ 0.00000200 / 0.00000100 CPU ]
create_token_pointer_array/test_create_token_pointer_array_memory_allocation[ OK ] [ 0.00000200 / 0.00000200 CPU ]
3 of 3 (100%) tests successful, 0 (0%) test skipped.
Running test suite with seed 0x8614a0d1...
snek_zero_out/test_zero_out_integer [ OK ] [ 0.00000100 / 0.00000100 CPU ]
snek_zero_out/test_zero_out_float [ OK ] [ 0.00000100 / 0.00000000 CPU ]
snek_zero_out/test_zero_out_bool [ OK ] [ 0.00000000 / 0.00000000 CPU ]
snek_zero_out/test_zero_out_nonzero_values[ OK ] [ 0.00000100 / 0.00000000 CPU ]
4 of 4 (100%) tests successful, 0 (0%) test skipped.
Running test suite with seed 0x2f6f82cf...
void-pointer/swap_ints [ OK ] [ 0.00000000 / 0.00000000 CPU ]
void-pointer/swap_ints_same [ OK ] [ 0.00000100 / 0.00000200 CPU ]
2 of 2 (100%) tests successful, 0 (0%) test skipped.
Running test suite with seed 0x62099957...
void-pointer/swap_str [ OK ] [ 0.00000100 / 0.00000100 CPU ]
void-pointer/test_swap_str_long [ OK ] [ 0.00000100 / 0.00000100 CPU ]
2 of 2 (100%) tests successful, 0 (0%) test skipped.
Running test suite with seed 0x56ef9f75...
swap/generic_ints [ OK ] [ 0.00000100 / 0.00000200 CPU ]
swap/generic_strings [ OK ] [ 0.00000100 / 0.00000100 CPU ]
swap/generic_struct [ OK ] [ 0.00000200 / 0.00000200 CPU ]
3 of 3 (100%) tests successful, 0 (0%) test skipped.
Running test suite with seed 0xdf070ee5...
snekstack/create_stack_small [ OK ] [ 0.00000200 / 0.00000200 CPU ]
snekstack/create_stack_large [ OK ] [ 0.00000100 / 0.00000100 CPU ]
2 of 2 (100%) tests successful, 0 (0%) test skipped.
Running test suite with seed 0xa0778dac...
snekstack/create_stack [ OK ] [ 0.00000100 / 0.00000200 CPU ]
snekstack/push_stack [ OK ] [ 0.00000100 / 0.00000200 CPU ]
snekstack/push_double_capacity [ OK ] [ 0.00000300 / 0.00000300 CPU ]
3 of 3 (100%) tests successful, 0 (0%) test skipped.
Running test suite with seed 0x1df97623...
snekstack/create_stack [ OK ] [ 0.00000200 / 0.00000200 CPU ]
snekstack/push_stack [ OK ] [ 0.00000200 / 0.00000300 CPU ]
snekstack/pop_stack [ OK ] [ 0.00000100 / 0.00000100 CPU ]
snekstack/pop_stack_empty [ OK ] [ 0.00000200 / 0.00000100 CPU ]
4 of 4 (100%) tests successful, 0 (0%) test skipped.
Running test suite with seed 0xc67f74fb...
snekstack/create_stack [ OK ] [ 0.00000100 / 0.00000100 CPU ]
snekstack/push_stack [ OK ] [ 0.00000200 / 0.00000200 CPU ]
snekstack/pop_stack [ OK ] [ 0.00000100 / 0.00000200 CPU ]
3 of 3 (100%) tests successful, 0 (0%) test skipped.
Running test suite with seed 0xb607698d...
snekstack/heterogenous_stack [ OK ] [ 0.00000200 / 0.00000100 CPU ]
1 of 1 (100%) tests successful, 0 (0%) test skipped.
Running test suite with seed 0x4804e928...
snekstack/multiple_types_stack [ OK ] [ 0.00000200 / 0.00000300 CPU ]
1 of 1 (100%) tests successful, 0 (0%) test skipped.
Running test suite with seed 0xa5a077a3...
object-integer-def/integer_constant [ OK ] [ 0.00000000 / 0.00000100 CPU ]
object-integer-def/integer_obj [ OK ] [ 0.00000100 / 0.00000100 CPU ]
2 of 2 (100%) tests successful, 0 (0%) test skipped.
Running test suite with seed 0x0a7224ef...
object-integer/positive [ OK ] [ 0.00000100 / 0.00000200 CPU ]
object-integer/zero [ OK ] [ 0.00000100 / 0.00000100 CPU ]
object-integer/negative [ OK ] [ 0.00000100 / 0.00000100 CPU ]
3 of 3 (100%) tests successful, 0 (0%) test skipped.
Running test suite with seed 0x748c3598...
object-float/positive [ OK ] [ 0.00000100 / 0.00000200 CPU ]
object-float/zero [ OK ] [ 0.00000100 / 0.00000100 CPU ]
object-float/negative [ OK ] [ 0.00000100 / 0.00000100 CPU ]
3 of 3 (100%) tests successful, 0 (0%) test skipped.
Running test suite with seed 0x4632a6a6...
object-string/copies_value [ OK ] [ 0.00000200 / 0.00000200 CPU ]
1 of 1 (100%) tests successful, 0 (0%) test skipped.
Running test suite with seed 0x386735e2...
object-vector/returns_null [ OK ] [ 0.00000100 / 0.00000200 CPU ]
object-vector/multiple_objects [ OK ] [ 0.00000200 / 0.00000200 CPU ]
object-vector/same_object [ OK ] [ 0.00000100 / 0.00000200 CPU ]
3 of 3 (100%) tests successful, 0 (0%) test skipped.
Running test suite with seed 0x4e6badf5...
object-array/empty [ OK ] [ 0.00000100 / 0.00000100 CPU ]
object-array/calloc [ OK ] [ 0.00000100 / 0.00000100 CPU ]
2 of 2 (100%) tests successful, 0 (0%) test skipped.
Running test suite with seed 0xe9625367...
object-array/set_and_get [ OK ] [ 0.00000200 / 0.00000200 CPU ]
object-array/set_outside [ OK ] [ 0.00000200 / 0.00000300 CPU ]
object-array/get_outside [ OK ] [ 0.00000200 / 0.00000200 CPU ]
3 of 3 (100%) tests successful, 0 (0%) test skipped.
Running test suite with seed 0xa7022b54...
object-length/integer [ OK ] [ 0.00000200 / 0.00000100 CPU ]
object-length/float [ OK ] [ 0.00000200 / 0.00000200 CPU ]
object-length/string [ OK ] [ 0.00000200 / 0.00000200 CPU ]
object-length/vector [ OK ] [ 0.00000200 / 0.00000200 CPU ]
object-length/array [ OK ] [ 0.00000200 / 0.00000100 CPU ]
5 of 5 (100%) tests successful, 0 (0%) test skipped.
[100%] Built target run