From 1c0cdec73674eac462a08599444391b831b6d8c8 Mon Sep 17 00:00:00 2001 From: Jidong Xiao Date: Mon, 18 Sep 2023 22:00:18 -0400 Subject: [PATCH] adjusting section 2 --- lectures/06_memory/README.md | 38 ++++++++++++++++++++++++------------ 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/lectures/06_memory/README.md b/lectures/06_memory/README.md index 74d3d10..430a694 100644 --- a/lectures/06_memory/README.md +++ b/lectures/06_memory/README.md @@ -29,7 +29,8 @@ Dynamic memory is:
-
+
+ 
 int * p = new int;
 *p = 17;
 cout << *p << endl;
@@ -44,25 +45,38 @@ p = temp;
 cout << *p << " " << *q << endl;
 delete p;
 delete q;
-
+
+
heap
+ -- The expression new int asks the system for a new chunk of memory that is large enough to hold an integer -and returns the address of that memory. Therefore, the statement int * p = new int; allocates memory -from the heap and stores its address in the pointer variable p. -- The statement delete p; takes the integer memory pointed by p and returns it to the system for re-use. -- This memory is allocated from and returned to a special area of memory called the heap. By contrast, local -variables and function parameters are placed on the stack as discussed last lecture. -- In between the new and delete statements, the memory is treated just like memory for an ordinary variable, +- The expression *new int* asks the system for a new chunk of memory that is large enough to hold an integer +and returns the address of that memory. Therefore, the statement + +```cpp +int * p = new int; +``` + +allocates memory from the heap and stores its address in the pointer variable *p*. + +- The statement + +```cpp +delete p; +``` + +takes the integer memory pointed by *p* and returns it to the system for re-use. +- This memory is allocated from and returned to a special area of memory called the **heap**. By contrast, local +variables and function parameters are placed on the stack. +- In between the *new* and *delete* statements, the memory is treated just like memory for an ordinary variable, except the only way to access it is through pointers. Hence, the manipulation of pointer variables and values is -similar to the examples covered in Lecture 5 except that there is no explicitly named variable for that memory +similar to the examples covered in the pointers lecture except that there is no explicitly named variable for that memory other than the pointer variable. -- Dynamic allocation of primitives like ints and doubles is not very interesting or significant. What’s more -important is dynamic allocation of arrays and objects. +- Dynamic allocation of primitives like ints and doubles is not very interesting or significant. What’s more important is dynamic allocation of arrays and class objects. ## 6.3 Exercises