diff --git a/.zk/notebook.db b/.zk/notebook.db index 53d16bd..d2fc3c1 100644 Binary files a/.zk/notebook.db and b/.zk/notebook.db differ diff --git a/zk/Heap_memory.md b/zk/Heap_memory.md new file mode 100644 index 0000000..c03a6ea --- /dev/null +++ b/zk/Heap_memory.md @@ -0,0 +1,10 @@ +--- +id: 18bl +title: Heap_memory +tags: [] +created: Saturday, April 20, 2024 +--- + +# Heap_memory + +## Related notes diff --git a/zk/Stack_memory.md b/zk/Stack_memory.md index c39abc9..9310fb7 100644 --- a/zk/Stack_memory.md +++ b/zk/Stack_memory.md @@ -7,8 +7,18 @@ created: Tuesday, April 16, 2024 # Stack memory -Along with heap memory, programs make use of _stack memory_ during the runtime -of programs. +## Summary + +- Along with heap memory, programs make use of _stack memory_ during the runtime + of programs. + +- Specifically used to keep track of memory used during function executions + within a given scope: control flow, local variables, returned value. + +- Uses a LIFO data-structure + +- Best suited for small-sized singular values rather than large or complex data + types ## Stack memory @@ -25,6 +35,30 @@ from the top down. A **stack pointer** is used to manage the items in the stack. This value is stored in a [[CPU_architecture#registers|register]] and its value is the memory -address of the item currently on the top of the stack. +address of the item currently on the top of the stack. Note: _not_ the value +itself, the _address_ of the value. + +When a new value is added to the top of the stack, the pointer's value is +adjusted to increase the size of the stack and make room for the new value. So +this will be a new memory address for the latest item. When an item is removed, +the reverse happens. The previously top address is cleared and the pointer +points to the new top item. + +### Role in program execution + +1. Function call management: a stack records the key information pertaining to + the execution of a function: parameters, local variables, the location of the + address for the returned value. + +2. Local variable storage: storing the variables local to a function's scope and + removing them once the function returns, avoiding + [[Memory_leaks|memory leaks]] + +3. Control flow: record from where a function is called and where its return + value should be stored, meaning the right value is always returned no matter + how nested it is. + +4. Recursion: individuating each call of a function that calls itself and its + given step to avoid infinite loops ## Related notes