Skip to content

Commit 6570f20

Browse files
author
ranch
committed
added headers
1 parent 3520a66 commit 6570f20

1 file changed

Lines changed: 4 additions & 0 deletions

File tree

  • docs/Teaching/C++/CS-2370/Projects/Project4

docs/Teaching/C++/CS-2370/Projects/Project4/grow.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Grow Explanation (And general memory management)
22

3+
## Pointer vs Data
4+
35
In the following example, we have some stack allocated object called dynarray.
46

57
Because it is allocated on the stack, any of its locals are also allocated on the stack with it. That is to say, the pointer defined in dynarray, data*, is a local variable. When the dynarray object goes out of scope, all of its locals go out of scope along with it. Not the data pointed to by its local data members.
@@ -14,6 +16,8 @@ int* data = new int[capacity]
1416

1517
The 'new' keyword returns the address generated by the newing of an integer array of size capacity. data is a simple variable that holds the address. As such, when you call delete, nothing __happens__ to the data variable itself. The object stored at the address pointed to by data is deallocated. data itself is still pointing to that region, but the data there no longer belongs to it. (Note: Dangling pointers are when a pointer that holds an address that we deallocated is still accessible.)
1618

19+
## How grow works
20+
1721
So, in the example below, for the grow function you're expected to implement, we leverage this information and perform the following tasks:
1822

1923
1) Create a temporary array. The project requires our array to be 1.6x the size of the previous. So, we increase our capacity, initially set to size CHUNK, and then for every time we grow, we multiply capacity by 1.6. Then allocate a new array using our new capacity. ```int* temp = new int[capacity];```

0 commit comments

Comments
 (0)