From ba8635a2cd8f59efecb733e2ba17e962a51e6aa5 Mon Sep 17 00:00:00 2001 From: Jidong Xiao Date: Fri, 26 Jan 2024 13:28:58 -0500 Subject: [PATCH] more on static memory and stack memory --- lectures/06_memory/README.md | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/lectures/06_memory/README.md b/lectures/06_memory/README.md index d0e4d35..23a4b2e 100644 --- a/lectures/06_memory/README.md +++ b/lectures/06_memory/README.md @@ -26,17 +26,21 @@ your accommodations for the test. ## 6.1 Three Types of Memory -- Automatic memory: memory allocation inside a function when you create a variable. This allocates space for -local variables in functions (on the stack) and deallocates it when variables go out of scope. For example: +- Stack memory: a region of a computer's memory that is used for the storage of local variables and function call information. When you create variables inside a function, such variables are called local variables and local variables will be stored on the stack and get deallocated when variables go out of scope. For example: + ```cpp -int x; -double y; +void func() { + int x; + double y; +} ``` -- Static memory: variables allocated statically (with the keyword static). They are are not eliminated when -they go out of scope. They retain their values, but are only accessible within the scope where they are defined. For example: + +- Static memory: variables allocated statically (with the keyword static), located at the data segment of the program's memory. They are are not eliminated when they go out of scope. They retain their values throughout the entire program execution, but are only accessible within the scope where they are defined. For example: + ```cpp static int counter; ``` + - Dynamic memory: explicitly allocated (on the heap) as needed. This is our focus for today. ## 6.2 Dynamic Memory