From e451cf2eec4dc7b345a1628df29c9c1938b42f31 Mon Sep 17 00:00:00 2001 From: Arnaud de Grandmaison Date: Wed, 28 Aug 2024 18:38:29 +0200 Subject: [PATCH] [LP] dynamic memory allocator: misc small fixes or rewording. --- .../1_dynamic_memory_allocation.md | 7 ++++--- .../2_designing_a_dynamic_memory_allocator.md | 4 ++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/content/learning-paths/cross-platform/dynamic-memory-allocator/1_dynamic_memory_allocation.md b/content/learning-paths/cross-platform/dynamic-memory-allocator/1_dynamic_memory_allocation.md index 539ceb5b0..7bb22265a 100644 --- a/content/learning-paths/cross-platform/dynamic-memory-allocator/1_dynamic_memory_allocation.md +++ b/content/learning-paths/cross-platform/dynamic-memory-allocator/1_dynamic_memory_allocation.md @@ -58,15 +58,16 @@ The C library looks for a chunk of memory with a size of at least X bytes within parameter passed to `malloc`. For instance, on Ubuntu Linux, this is done by GLIBC. The example at the top of the page is trivial, of course. As it is we could just -statically allocate both integers like this: +initialize both `a` and `b` at compilation time like this: ```C void fn() { - int a, b = 0; + int a = 0; + int *b = NULL; } ``` -Variables `a` and `b` work fine if they are not needed outside of the function. In other +This works fine as `a` and `b` are not needed outside of the function or in other words, if the lifetime of the data is equal to that of the function. A more complex example (shown below) demonstrates when this is not the case, and the values diff --git a/content/learning-paths/cross-platform/dynamic-memory-allocator/2_designing_a_dynamic_memory_allocator.md b/content/learning-paths/cross-platform/dynamic-memory-allocator/2_designing_a_dynamic_memory_allocator.md index 1bf5153d4..40e6740ad 100644 --- a/content/learning-paths/cross-platform/dynamic-memory-allocator/2_designing_a_dynamic_memory_allocator.md +++ b/content/learning-paths/cross-platform/dynamic-memory-allocator/2_designing_a_dynamic_memory_allocator.md @@ -12,7 +12,7 @@ To begin, decide which functions your memory allocator will provide. We have already described `malloc` and `free` but there are more provided by the [C library](https://en.cppreference.com/w/c/memory). -This demo assumes you just need `malloc` and `free`. The new implementations will +This learning path assumes you just need `malloc` and `free`. The new implementations will be called `simple_malloc` and `simple_free`. Start with just these two functions and write out their behaviors. @@ -96,7 +96,7 @@ range = 0x0 + 4 = 0x4; Pointer: 0x4 Size: N-4 Allocated: False -range = 0x4 + (N-4) = 1 beyond the end of the heap, so the walk is finished. +range = 0x4 + (N-4) = N beyond the end of the heap, so the walk is finished. ``` `simple_free` uses the pointer given to it to find the range it needs to de-allocate.