From e0f8cdb3d3b8d8f513f90a9fbab51511c4779db7 Mon Sep 17 00:00:00 2001 From: Jet Hughes Date: Thu, 11 Aug 2022 11:39:21 +1200 Subject: [PATCH] vault backup: 2022-08-11 11:39:21 --- .../notes/10-intro-to-c-arrays-malloc-free.md | 32 ++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/content/notes/10-intro-to-c-arrays-malloc-free.md b/content/notes/10-intro-to-c-arrays-malloc-free.md index fafa0db7f..b5390e723 100644 --- a/content/notes/10-intro-to-c-arrays-malloc-free.md +++ b/content/notes/10-intro-to-c-arrays-malloc-free.md @@ -62,6 +62,36 @@ uint_t byte_array[1024]; ## malloc - "memory allocate" -- +- always in bytes + +```c +uint8_t *space = malloc(16); //give me 16 bytes of space +uint64_t *block = malloc(sizeof(*uint64_t) * 16); +uint64_t *another = malloc(sizeof(*another) * 16); //better: can change type without breaking it +``` +## free +**when you have finished using an allocated block you must free that block** + +```c +uint64_t *block = malloc(sizeof(*block) * 16); //better: can change type without breaking it +//do something +free(block); +``` + +- never free memory you didn't allocate +- never free the same memory twice +- must call free before you lose the pointer to the block of memory + +## bugs +- malloc'd block is too small + - buffer-overrun +- didn't free a block + - memory leak +- malloc and free small units a large number of times + - memory fragmentation +- free same block more than once + - corrupt memory management routines +- using memory after it has been freed + - it have been re-used by malloc \ No newline at end of file