Skip to content

Commit 66c62c8

Browse files
committedMar 9, 2022
Added all Section 12 material.
1 parent 5518343 commit 66c62c8

File tree

40 files changed

+668
-102
lines changed

40 files changed

+668
-102
lines changed
 

‎Section12_Pointers_and_Reference/README.md

+289-20
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
1. A pointer variable can store __________ .
2+
3+
- [ ] integer values
4+
- [ ] double values
5+
- [x] addresses of other variables
6+
- [ ] string values
7+
8+
2. In order to determine the address of a variable in C++, we can use the __________ operator.
9+
10+
- [ ] `@`
11+
- [ ] `++`
12+
- [ ] `*`
13+
- [x] `&`
14+
15+
16+
3. Pointer variables must always be __________ before they are used.
17+
18+
- [ ] dereferenced
19+
- [ ] static
20+
- [x] initialized
21+
- [ ] checked
22+
23+
4. In order to follow a pointer and access the data it is pointing to, we must __________ the pointer using the __________ operator.
24+
25+
- [ ] use, @
26+
- [ ] access, &
27+
- [x] dereference, *
28+
- [ ] reference, *
29+
30+
5. Pointer can be used to dynamically allocate storage from the __________ at __________.
31+
32+
- [ ] stack, compile-time
33+
- [ ] stack, run-time
34+
- [x] heap or free store, run-time
35+
- [ ] heap or free store, compile-time
36+
37+
6. When using raw pointers and dynamica storage allocation, we must always de-allocate the used storage by using __________ to prevent __________.
38+
39+
- [x] delete, memory leaks
40+
- [ ] free, buffer overruns
41+
- [ ] delete, stack overflows
42+
- [ ] new, memory leaks
43+
44+
7. __________ and pointers can be used interchangeably in many contexts.
45+
46+
- [ ] Function calls
47+
- [ ] Classes
48+
- [ ] Vectors
49+
- [x] Array names
50+
51+
8. What types of variables can ptr store given the following declaraion below?
52+
`int **ptr;`
53+
54+
- [ ] integers
55+
- [ ] addresses of integers
56+
- [x] addresses of pointers to integers
57+
- [ ] any type of integer
58+
59+
9. What does the following code snippet display?
60+
61+
```c++
62+
int *data = new int[5];
63+
64+
for (int i=0; i<5; i++)
65+
*(data + i) = i*2;
66+
67+
for (int i=0; i<4; i++)
68+
cout << data[i] << " ";
69+
cout << endl;
70+
71+
delete [] data;
72+
```
73+
74+
- [ ] 0 1 2 3 4
75+
- [x] 0 2 4 6 8
76+
- [ ] 2 4 6 8 10
77+
- [ ] garbage data
78+
79+
10. Given the following pointer decalrations, what can you say about ptr1 and ptr2?
80+
81+
```c++
82+
int *ptr1;
83+
int *ptr2 {nullptr};
84+
```
85+
86+
- [ ] ptr1 and ptr2 are both contains the address 0
87+
- [ ] ptr1 and ptr2 are both initialized
88+
- [x] ptr1 is uninitialized and ptr2 is initialized
89+
- [ ] ptr1 and ptr2 are both uninitalized
Binary file not shown.

‎Section12_Pointers_and_Reference/section12_source_code/AraysAndPointers/main.cpp

+5-4
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ using namespace std;
44

55
int main() {
66

7+
// Initialize scores array
78
int scores[] {100, 95, 89};
89

9-
cout << "Value of scores: " << scores << endl;
10+
cout << "Value of scores: " << scores << endl; // Output: address of array first element
1011

1112
int *score_ptr {scores};
1213
cout << "Value of score_ptr: " << score_ptr << endl;
@@ -23,13 +24,13 @@ int main() {
2324

2425
cout << "\nPointer offset notation-------------------------" << endl;
2526
cout << *score_ptr << endl; // *address // Output: 100
26-
cout << *(score_ptr + 1) << endl; // *(address + 4) // Output: 95
27-
cout << *(score_ptr +2) << endl; // *(address + 8) // Output: 89
27+
cout << *(score_ptr + 1) << endl; // *(address + 4 bytes) // Output: 95
28+
cout << *(score_ptr + 2) << endl; // *(address + 8 bytes) // Output: 89
2829

2930
cout << "\nArray offset notation-------------------------" << endl;
3031
cout << *scores << endl; // Output: 100
3132
cout << *(scores + 1) << endl; // Output: 95
32-
cout << *(scores +2) << endl; // Output: 89
33+
cout << *(scores + 2) << endl; // Output: 89
3334

3435
cout << endl;
3536
return 0;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>CFBundleDevelopmentRegion</key>
6+
<string>English</string>
7+
<key>CFBundleIdentifier</key>
8+
<string>com.apple.xcode.dsym.main</string>
9+
<key>CFBundleInfoDictionaryVersion</key>
10+
<string>6.0</string>
11+
<key>CFBundlePackageType</key>
12+
<string>dSYM</string>
13+
<key>CFBundleSignature</key>
14+
<string>????</string>
15+
<key>CFBundleShortVersionString</key>
16+
<string>1.0</string>
17+
<key>CFBundleVersion</key>
18+
<string>1</string>
19+
</dict>
20+
</plist>
Binary file not shown.

‎Section12_Pointers_and_Reference/section12_source_code/Challenge/main.cpp

+46-19
Original file line numberDiff line numberDiff line change
@@ -40,27 +40,54 @@
4040

4141
using namespace std;
4242

43+
int *apply_all(const int *const arr1, size_t const arr1_size, const int *const arr2, size_t const arr2_size) {
44+
45+
int *new_arr {nullptr}; // Pointer declartation
46+
new_arr = new int[arr1_size * arr2_size]; // Assign memory in the heap
47+
48+
size_t i{0};
49+
for (size_t j{0}; j < arr2_size; j++) {
50+
for (size_t k{0}; k < arr1_size; k++) {
51+
new_arr[i] = arr1[k] * arr2[j];
52+
i++;
53+
}
54+
}
55+
56+
return new_arr;
57+
58+
}
59+
60+
61+
void print(const int *const arr, size_t arr_size) {
62+
63+
for (size_t i{0}; i < arr_size; i++)
64+
cout << arr[i] << " ";
65+
// cout << *(arr + i);
66+
cout << endl;
67+
68+
}
69+
4370
int main() {
44-
const size_t array1_size {5};
45-
const size_t array2_size {3};
46-
47-
int array1[] {1,2,3,4,5};
48-
int array2[] {10,20,30};
49-
50-
cout << "Array 1: " ;
51-
print(array1,array1_size);
52-
53-
cout << "Array 2: " ;
54-
print(array2,array2_size);
55-
56-
int *results = apply_all(array1, array1_size, array2, array2_size);
57-
constexpr size_t results_size {array1_size * array2_size};
71+
const size_t array1_size {5};
72+
const size_t array2_size {3};
5873

59-
cout << "Result: " ;
60-
print(results, results_size);
61-
62-
cout << endl;
74+
int array1[] {1,2,3,4,5};
75+
int array2[] {10,20,30};
76+
77+
cout << "Array 1: " ;
78+
print(array1,array1_size);
79+
80+
cout << "Array 2: " ;
81+
print(array2,array2_size);
82+
83+
int *results = apply_all(array1, array1_size, array2, array2_size);
84+
constexpr size_t results_size {array1_size * array2_size};
85+
cout << "Result: " ;
86+
print(results, results_size);
87+
88+
delete [] results;
6389

64-
return 0;
90+
cout << endl;
91+
return 0;
6592
}
6693

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>CFBundleDevelopmentRegion</key>
6+
<string>English</string>
7+
<key>CFBundleIdentifier</key>
8+
<string>com.apple.xcode.dsym.main</string>
9+
<key>CFBundleInfoDictionaryVersion</key>
10+
<string>6.0</string>
11+
<key>CFBundlePackageType</key>
12+
<string>dSYM</string>
13+
<key>CFBundleSignature</key>
14+
<string>????</string>
15+
<key>CFBundleShortVersionString</key>
16+
<string>1.0</string>
17+
<key>CFBundleVersion</key>
18+
<string>1</string>
19+
</dict>
20+
</plist>
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>CFBundleDevelopmentRegion</key>
6+
<string>English</string>
7+
<key>CFBundleIdentifier</key>
8+
<string>com.apple.xcode.dsym.main</string>
9+
<key>CFBundleInfoDictionaryVersion</key>
10+
<string>6.0</string>
11+
<key>CFBundlePackageType</key>
12+
<string>dSYM</string>
13+
<key>CFBundleSignature</key>
14+
<string>????</string>
15+
<key>CFBundleShortVersionString</key>
16+
<string>1.0</string>
17+
<key>CFBundleVersion</key>
18+
<string>1</string>
19+
</dict>
20+
</plist>
Binary file not shown.

‎Section12_Pointers_and_Reference/section12_source_code/Dereference/main.cpp

+23-12
Original file line numberDiff line numberDiff line change
@@ -10,42 +10,53 @@ using namespace std;
1010
int main() {
1111

1212
int score {100};
13+
// Initialize score_ptr to point at the location of score
1314
int *score_ptr {&score};
1415

15-
cout << *score_ptr << endl; // 100
16+
// Dereference score_ptr
17+
cout << *score_ptr << endl; // Output: 100
1618

19+
// Dereference score_ptr and put value 200 inside
1720
*score_ptr = 200;
1821

19-
cout << *score_ptr << endl; // 200
20-
cout << score << endl; // 200
22+
// Dereference score_ptr
23+
cout << *score_ptr << endl; // Output: 200
24+
cout << score << endl; // Output: 200
25+
2126

2227
cout << "\n------------------------------" << endl;
2328
double high_temp {100.7};
2429
double low_temp {37.4};
2530
double *temp_ptr {&high_temp};
2631

27-
cout << *temp_ptr << endl; // 100.7
28-
temp_ptr = &low_temp;
29-
cout << *temp_ptr << endl; // 37.4
32+
cout << *temp_ptr << endl; // Output: 100.7
33+
temp_ptr = &low_temp; // Change where pointer is pointing to
34+
cout << *temp_ptr << endl; // Output: 37.4
35+
3036

3137
cout << "\n------------------------------" << endl;
3238

3339
string name {"Frank"};
40+
// Initialize string_ptr to point at the location of name
3441
string *string_ptr {&name};
3542

36-
cout << *string_ptr << endl; // Frank
37-
name = "James";
38-
cout << *string_ptr << endl; // James
39-
//
43+
// Dereference string_ptr
44+
cout << *string_ptr << endl; // Output: Frank
45+
name = "James"; // Change variable value directly
46+
cout << *string_ptr << endl; // Output: James
47+
*string_ptr = "Anthony"; // Dereference string_ptr and put value Anthony inside
48+
cout << *string_ptr << endl; // Output: Anthony
49+
50+
4051
cout << "\n------------------------------" << endl;
4152
vector<string> stooges {"Larry", "Moe", "Curly"};
4253
vector<string> *vector_ptr {nullptr};
4354

4455
vector_ptr = &stooges;
4556

46-
cout << "&stooges:\t" << vector_ptr << endl; // address
57+
cout << "&stooges:\t" << vector_ptr << endl; // Output: address
4758

48-
cout << "First stooge: " << (*vector_ptr).at(0) << endl; // Larry
59+
cout << "First stooge: " << (*vector_ptr).at(0) << endl; // Output: Larry
4960

5061
cout << "Stooges: ";
5162
for (auto stooge: *vector_ptr)

‎Section12_Pointers_and_Reference/section12_source_code/DynamicMemory/main.cpp

+7-4
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ using namespace std;
77
int main() {
88

99
int *int_ptr {nullptr};
10-
int_ptr = new int; // allocate the int on the heap
11-
cout << int_ptr << endl; // use it
12-
delete int_ptr; // release it
10+
int_ptr = new int; // allocate the int dynamically on the heap
11+
cout << int_ptr << endl; // Output: address in the heap
12+
// Can use int_ptr now
13+
delete int_ptr; // release it
1314

1415
size_t size{0};
1516
double *temp_ptr {nullptr};
@@ -19,7 +20,9 @@ int main() {
1920

2021
temp_ptr = new double[size]; // allocate the storage on the heap
2122
cout << temp_ptr << endl; // use it
22-
delete [] temp_ptr; // release it
23+
// Memory leak if temp_ptr = nullptr;
24+
// Now we lose the only way to access the memeory in the heap
25+
delete [] temp_ptr; // release it
2326

2427
cout << endl;
2528
return 0;
Binary file not shown.

‎Section12_Pointers_and_Reference/section12_source_code/PassingPointers/main.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,4 @@ int main() {
2424

2525
cout << endl;
2626
return 0;
27-
}
27+
}
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>CFBundleDevelopmentRegion</key>
6+
<string>English</string>
7+
<key>CFBundleIdentifier</key>
8+
<string>com.apple.xcode.dsym.main</string>
9+
<key>CFBundleInfoDictionaryVersion</key>
10+
<string>6.0</string>
11+
<key>CFBundlePackageType</key>
12+
<string>dSYM</string>
13+
<key>CFBundleSignature</key>
14+
<string>????</string>
15+
<key>CFBundleShortVersionString</key>
16+
<string>1.0</string>
17+
<key>CFBundleVersion</key>
18+
<string>1</string>
19+
</dict>
20+
</plist>
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>CFBundleDevelopmentRegion</key>
6+
<string>English</string>
7+
<key>CFBundleIdentifier</key>
8+
<string>com.apple.xcode.dsym.main</string>
9+
<key>CFBundleInfoDictionaryVersion</key>
10+
<string>6.0</string>
11+
<key>CFBundlePackageType</key>
12+
<string>dSYM</string>
13+
<key>CFBundleSignature</key>
14+
<string>????</string>
15+
<key>CFBundleShortVersionString</key>
16+
<string>1.0</string>
17+
<key>CFBundleVersion</key>
18+
<string>1</string>
19+
</dict>
20+
</plist>
Binary file not shown.

0 commit comments

Comments
 (0)
Please sign in to comment.