Dynamic Storage Exercise. We saw that by int i; while (std::cin i)... we can read inputs as long as...
-
Upload
lillian-watts -
Category
Documents
-
view
217 -
download
0
description
Transcript of Dynamic Storage Exercise. We saw that by int i; while (std::cin i)... we can read inputs as long as...
![Page 1: Dynamic Storage Exercise. We saw that by int i; while (std::cin i)... we can read inputs as long as there are more available in std::cin. Your task.](https://reader035.fdocuments.us/reader035/viewer/2022081514/5a4d1b767f8b9ab0599b71fb/html5/thumbnails/1.jpg)
Dynamic Storage Exercise
![Page 2: Dynamic Storage Exercise. We saw that by int i; while (std::cin i)... we can read inputs as long as there are more available in std::cin. Your task.](https://reader035.fdocuments.us/reader035/viewer/2022081514/5a4d1b767f8b9ab0599b71fb/html5/thumbnails/2.jpg)
(From: Script Exercise 158.a) 2
Dynamic Storage Exercise
We saw that byint i;while (std::cin >> i) ...
we can read inputs as long as there are more available in std::cin.
Your task is to write a code snippet which reads inputs as described above, and which then stores these inputs in an array. For this exercise you are not allowed to use the Standard Library (i.e. no std::vector).
To achieve this you will have to use new[] and delete[].
![Page 3: Dynamic Storage Exercise. We saw that by int i; while (std::cin i)... we can read inputs as long as there are more available in std::cin. Your task.](https://reader035.fdocuments.us/reader035/viewer/2022081514/5a4d1b767f8b9ab0599b71fb/html5/thumbnails/3.jpg)
(From: Script Exercise 158.a) 3
Dynamic Storage Solution
• Idea:1. Allocate some range (using new[])
2. As soon as range full, allocate larger range (using new[])
3. Copy over initial range
4. Delete initial range (using delete[])
5. Go back to 2. with newly generated memory
![Page 4: Dynamic Storage Exercise. We saw that by int i; while (std::cin i)... we can read inputs as long as there are more available in std::cin. Your task.](https://reader035.fdocuments.us/reader035/viewer/2022081514/5a4d1b767f8b9ab0599b71fb/html5/thumbnails/4.jpg)
(From: Script Exercise 158.a) 4
Dynamic Storage Solution
• Idea:1. Allocate some range (using new[])
2. As soon as range full, allocate larger range (using new[])
3. Copy over initial range
4. Delete initial range (using delete[])
5. Go back to 2. with newly generated memory
4 2 7
![Page 5: Dynamic Storage Exercise. We saw that by int i; while (std::cin i)... we can read inputs as long as there are more available in std::cin. Your task.](https://reader035.fdocuments.us/reader035/viewer/2022081514/5a4d1b767f8b9ab0599b71fb/html5/thumbnails/5.jpg)
(From: Script Exercise 158.a) 5
Dynamic Storage Solution
• Idea:1. Allocate some range (using new[])
2. As soon as range full, allocate larger range (using new[])
3. Copy over initial range
4. Delete initial range (using delete[])
5. Go back to 2. with newly generated memory
4 2 7
![Page 6: Dynamic Storage Exercise. We saw that by int i; while (std::cin i)... we can read inputs as long as there are more available in std::cin. Your task.](https://reader035.fdocuments.us/reader035/viewer/2022081514/5a4d1b767f8b9ab0599b71fb/html5/thumbnails/6.jpg)
(From: Script Exercise 158.a) 6
Dynamic Storage Solution
• Idea:1. Allocate some range (using new[])
2. As soon as range full, allocate larger range (using new[])
3. Copy over initial range
4. Delete initial range (using delete[])
5. Go back to 2. with newly generated memory
4 2 7
![Page 7: Dynamic Storage Exercise. We saw that by int i; while (std::cin i)... we can read inputs as long as there are more available in std::cin. Your task.](https://reader035.fdocuments.us/reader035/viewer/2022081514/5a4d1b767f8b9ab0599b71fb/html5/thumbnails/7.jpg)
(From: Script Exercise 158.a) 7
Dynamic Storage Solution
• Idea:1. Allocate some range (using new[])
2. As soon as range full, allocate larger range (using new[])
3. Copy over initial range
4. Delete initial range (using delete[])
5. Go back to 2. with newly generated memory
4 2 7 4
![Page 8: Dynamic Storage Exercise. We saw that by int i; while (std::cin i)... we can read inputs as long as there are more available in std::cin. Your task.](https://reader035.fdocuments.us/reader035/viewer/2022081514/5a4d1b767f8b9ab0599b71fb/html5/thumbnails/8.jpg)
(From: Script Exercise 158.a) 8
Dynamic Storage Solution
• Idea:1. Allocate some range (using new[])
2. As soon as range full, allocate larger range (using new[])
3. Copy over initial range
4. Delete initial range (using delete[])
5. Go back to 2. with newly generated memory
4 2 7 24
![Page 9: Dynamic Storage Exercise. We saw that by int i; while (std::cin i)... we can read inputs as long as there are more available in std::cin. Your task.](https://reader035.fdocuments.us/reader035/viewer/2022081514/5a4d1b767f8b9ab0599b71fb/html5/thumbnails/9.jpg)
(From: Script Exercise 158.a) 9
Dynamic Storage Solution
• Idea:1. Allocate some range (using new[])
2. As soon as range full, allocate larger range (using new[])
3. Copy over initial range
4. Delete initial range (using delete[])
5. Go back to 2. with newly generated memory
4 2 7 24 7
![Page 10: Dynamic Storage Exercise. We saw that by int i; while (std::cin i)... we can read inputs as long as there are more available in std::cin. Your task.](https://reader035.fdocuments.us/reader035/viewer/2022081514/5a4d1b767f8b9ab0599b71fb/html5/thumbnails/10.jpg)
(From: Script Exercise 158.a) 10
Dynamic Storage Solution
• Idea:1. Allocate some range (using new[])
2. As soon as range full, allocate larger range (using new[])
3. Copy over initial range
4. Delete initial range (using delete[])
5. Go back to 2. with newly generated memory
4 2 7 24 7
![Page 11: Dynamic Storage Exercise. We saw that by int i; while (std::cin i)... we can read inputs as long as there are more available in std::cin. Your task.](https://reader035.fdocuments.us/reader035/viewer/2022081514/5a4d1b767f8b9ab0599b71fb/html5/thumbnails/11.jpg)
(From: Script Exercise 158.a) 11
Dynamic Storage Solution
• Idea:1. Allocate some range (using new[])
2. As soon as range full, allocate larger range (using new[])
3. Copy over initial range
4. Delete initial range (using delete[])
5. Go back to 2. with newly generated memory
24 74 2 7
![Page 12: Dynamic Storage Exercise. We saw that by int i; while (std::cin i)... we can read inputs as long as there are more available in std::cin. Your task.](https://reader035.fdocuments.us/reader035/viewer/2022081514/5a4d1b767f8b9ab0599b71fb/html5/thumbnails/12.jpg)
(From: Script Exercise 158.a) 12
Dynamic Storage Solution
• New range… How much larger?
• much larger Pro: ranges less often full copy ranges less oftenCon: larger memory consumption
• Important: Larger by a factor, not by a constant…• length_n = length_o * 2
length_n = length_o + 2
![Page 13: Dynamic Storage Exercise. We saw that by int i; while (std::cin i)... we can read inputs as long as there are more available in std::cin. Your task.](https://reader035.fdocuments.us/reader035/viewer/2022081514/5a4d1b767f8b9ab0599b71fb/html5/thumbnails/13.jpg)
(From: Script Exercise 158.a) 13
Dynamic Storage Solution• Larger by: a) factor 2 b) constant 2
elements Case a) Case b)
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
![Page 14: Dynamic Storage Exercise. We saw that by int i; while (std::cin i)... we can read inputs as long as there are more available in std::cin. Your task.](https://reader035.fdocuments.us/reader035/viewer/2022081514/5a4d1b767f8b9ab0599b71fb/html5/thumbnails/14.jpg)
(From: Script Exercise 158.a) 14
Dynamic Storage Solution• Larger by: a) factor 2 b) constant 2
elements Case a) Case b)
22 2
34 4
44 4
58 6
68 6
78 8
88 8
916 10
1016 10
1116 12
1216 12
1316 14
1416 14
1516 16
1616 16
1732 18
arbitr. chosen
![Page 15: Dynamic Storage Exercise. We saw that by int i; while (std::cin i)... we can read inputs as long as there are more available in std::cin. Your task.](https://reader035.fdocuments.us/reader035/viewer/2022081514/5a4d1b767f8b9ab0599b71fb/html5/thumbnails/15.jpg)
(From: Script Exercise 158.a) 15
Dynamic Storage Solution• Larger by: a) factor 2 b) constant 2
elements Case a) Case b)
22 2
34 4
44 4
58 6
68 6
78 8
88 8
916 10
1016 10
1116 12
1216 12
1316 14
1416 14
1516 16
1616 16
1732 18
Case a):
Significantly fewer resizings.
arbitr. chosen
![Page 16: Dynamic Storage Exercise. We saw that by int i; while (std::cin i)... we can read inputs as long as there are more available in std::cin. Your task.](https://reader035.fdocuments.us/reader035/viewer/2022081514/5a4d1b767f8b9ab0599b71fb/html5/thumbnails/16.jpg)
(From: Script Exercise 158.a) 16
Dynamic Storage Solution• Larger by: a) factor 2 b) constant 2
elements Case a) Case b)
22 2
34 4
44 4
58 6
68 6
78 8
88 8
916 10
1016 10
1116 12
1216 12
1316 14
1416 14
1516 16
1616 16
1732 18
arbitr. chosen
Each resizing means:
Copy WHOLE array.
Case a):
Significantly fewer resizings.
![Page 17: Dynamic Storage Exercise. We saw that by int i; while (std::cin i)... we can read inputs as long as there are more available in std::cin. Your task.](https://reader035.fdocuments.us/reader035/viewer/2022081514/5a4d1b767f8b9ab0599b71fb/html5/thumbnails/17.jpg)
(From: Script Exercise 158.a) 17
Dynamic Storage Solution• Larger by: a) factor 2 b) constant 2
elements Case a) Case b)
22 2
34 4
44 4
58 6
68 6
78 8
88 8
916 10
1016 10
1116 12
1216 12
1316 14
1416 14
1516 16
1616 16
1732 18
arbitr. chosen
Each resizing means:
Copy WHOLE array.
Case a):
Significantly fewer resizings.
Factor 2 is an arbitrary, but good choice.
![Page 18: Dynamic Storage Exercise. We saw that by int i; while (std::cin i)... we can read inputs as long as there are more available in std::cin. Your task.](https://reader035.fdocuments.us/reader035/viewer/2022081514/5a4d1b767f8b9ab0599b71fb/html5/thumbnails/18.jpg)
(From: Script Exercise 158.a) 18
Dynamic Storage Solution• And the code…
int n = 1; // current array sizeint k = 0; // number of elements read so far
// dynamically allocate arrayint* a = new int[n]; // this time, a is NOT a constant
// read into the arraywhile (std::cin >> a[k]) { if (++k == n) { // next element wouldn't fit; replace the array a by // a new one of twice the size int* b = new int[n*=2]; // get pointer to new array for (int i=0; i<k; ++i) // copy old array to new one b[i] = a[i]; delete[] a; // delete old array a = b; // let a point to new array }}
![Page 19: Dynamic Storage Exercise. We saw that by int i; while (std::cin i)... we can read inputs as long as there are more available in std::cin. Your task.](https://reader035.fdocuments.us/reader035/viewer/2022081514/5a4d1b767f8b9ab0599b71fb/html5/thumbnails/19.jpg)
19
By the way, …
• … this is exactly howmy_vec.push_back(...)
works. push_back is a member function.
• … all dynamic containers (vector, set, list, …) are based on new, delete!
![Page 20: Dynamic Storage Exercise. We saw that by int i; while (std::cin i)... we can read inputs as long as there are more available in std::cin. Your task.](https://reader035.fdocuments.us/reader035/viewer/2022081514/5a4d1b767f8b9ab0599b71fb/html5/thumbnails/20.jpg)
Vector…
![Page 21: Dynamic Storage Exercise. We saw that by int i; while (std::cin i)... we can read inputs as long as there are more available in std::cin. Your task.](https://reader035.fdocuments.us/reader035/viewer/2022081514/5a4d1b767f8b9ab0599b71fb/html5/thumbnails/21.jpg)
21
Dynamic Storage in Vectors
•Vectors store 3 pointers:begin: begin of memory
end: end of user-accessible part
end2: end of allocated part
4 2 7 24 7
begin end end2
![Page 22: Dynamic Storage Exercise. We saw that by int i; while (std::cin i)... we can read inputs as long as there are more available in std::cin. Your task.](https://reader035.fdocuments.us/reader035/viewer/2022081514/5a4d1b767f8b9ab0599b71fb/html5/thumbnails/22.jpg)
22
Dynamic Storage in Vectors
• Important for vectors:• In constructor: Set initial range• In copy-constructor: Don’t copy just pointers;
i.e. copy the ranges behind them• In operator=: Like copy-constructor, in
addition: i) prevent self-assignments ii) don’t forget to delete old
range