--- title: Storage TARGET DECK: Obsidian::STEM FILE TAGS: c17::storage tags: - c17 --- ## Dynamic Allocation The `` header provides the two most prominent functions used for managing dynamic memory: `malloc` and `free`. The former is used to allocate new memory whereas the latter is used to annihilate it. ```c void* malloc(size_t size); void free(void*); ``` %%ANKI Basic Which standard library header exposes `malloc`? Back: `` Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). END%% %%ANKI Basic Which standard library header exposes `free`? Back: `` Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). END%% %%ANKI Basic Why is `malloc` named the way it is? Back: It stands for **m**emory **alloc**ate. Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). END%% %%ANKI Basic What two functions are most prominently used for dynamic allocation? Back: `malloc` and `free`. Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). END%% %%ANKI Cloze Generally speaking, memory created with {`malloc`} should be deleted with {`free`}. Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). END%% %%ANKI Basic What three specialized variants of `malloc` are exposed by ``? Back: `calloc`, `realloc`, and `aligned_alloc`. Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). END%% %%ANKI Basic On success, what does a call to `malloc` return? Back: A `void*` pointer. Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). END%% %%ANKI Basic On failure, what does a call to `malloc` return? Back: A null pointer value. Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). END%% %%ANKI Basic What are the argument(s) to `malloc`? Back: The size (in bytes) of the block of memory to allocate. Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). END%% %%ANKI Basic What is wrong with the following code snippet? ```c double *d = (double*)malloc(sizeof *d); ``` Back: The return type of `malloc` should not be cast. Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). END%% %%ANKI Basic What is wrong with the following code snippet? ```c double *d = malloc(sizeof *d); ``` Back: N/A. This is valid. Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). END%% %%ANKI Basic What possible error can occur by explicitly casting the return type of `malloc`? Back: If `` isn't imported, older C compilers assume `malloc` returns an `int`. Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). END%% %%ANKI Basic Without ``, an older C compiler might assume what `malloc` declaration? Back: ```c int malloc(); ``` Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). END%% %%ANKI Basic Within ``, what function prototype does `malloc` have? Back: ```c void* malloc(size_t size); ``` Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). END%% %%ANKI Cloze Storage allocated through `malloc` is {uninitialized} and has {no type}. Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). END%% %%ANKI Basic Does the following invoke undefined behavior? Why or why not? ```c free(0); ``` Back: No. `free` ignores null pointer values. Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). END%% %%ANKI Basic Does the following invoke undefined behavior? Why or why not? ```c double d = 0.0; free(&d); ``` Back: Yes. `d` was not dynamically allocated. Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). END%% %%ANKI Basic Does the following invoke undefined behavior? Why or why not? ```c double* d = malloc(sizeof *d); free(&d); ``` Back: Yes. `d` was not dynamically allocated. Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). END%% %%ANKI Basic Does the following invoke undefined behavior? Why or why not? ```c double* d = malloc(sizeof *d); free(d); ``` Back: No. The address `d` pointed to was dynamically allocated. Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). END%% %%ANKI Basic Does the following invoke undefined behavior? Why or why not? ```c double* d = malloc(sizeof *d); free(d); d = 0; free(d); ``` Back: No. `free` ignores null pointer values. Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). END%% %%ANKI Basic Does the following invoke undefined behavior? Why or why not? ```c double* d = malloc(sizeof *d); free(d); free(d); ``` Back: Yes. `free` should not be invoked on already `free`'d data. Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). END%% %%ANKI Basic `malloc` and its variants all have the `size` parameter in what position? Back: The last position. Reference: Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020). END%% ## Bibliography * Jens Gustedt, _Modern C_ (Shelter Island, NY: Manning Publications Co, 2020).