You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: concepts/pointers/about.md
+12-9
Original file line number
Diff line number
Diff line change
@@ -13,9 +13,10 @@ With modern C++ there are also _smart pointers_, the basic type is not smart at
13
13
14
14
Before digging into the details, it's worth understanding the use of _pointers_.
15
15
_Pointers_ are a way to share an object's address with other parts of our program, which is useful for two major reasons:
16
+
16
17
1. Like _references_, pointers avoid copies and help to reduce the resource-footprint of your program.
17
-
2. Unlike _references_, pointers can be reassigned to different objects.
18
-
3. Pointers can also point to a null value, to indicate, that they currently do not point to any object.
18
+
1. Unlike _references_, pointers can be reassigned to different objects.
19
+
1. Pointers can also point to a null value, to indicate, that they currently do not point to any object.
19
20
20
21
## General Syntax
21
22
@@ -48,10 +49,10 @@ _Pointer arithmetic_ allows you to perform arithmetic operations on pointers, wh
48
49
Adding an integer to a pointer makes it point to a different element.
49
50
50
51
```cpp
51
-
// Stargate addresses
52
-
intgateAddresses[] = {462, 753, 218, 611, 977};
53
-
// 'ptr' points to the first element of 'gateAddresses'
54
-
int* ptr{gateAddresses};
52
+
// Stargate Coordinate Code
53
+
intgateCode[] = {462, 753, 218, 611, 977};
54
+
// 'ptr' points to the first element of 'gateCode'
55
+
int* ptr{&gateCode[0]};
55
56
// Accesses the third Stargate address through pointer arithmetic
56
57
int dialedAddress{*(ptr + 2)};
57
58
// Chevron encoded! Dialing Stargate address:
@@ -80,10 +81,13 @@ struct Superhero {
80
81
std::string superpower;
81
82
};
82
83
83
-
Superhero* dianaPrince = new Superhero;
84
+
Superhero wonder_woman{};
85
+
Superhero* dianaPrince = &wonder_woman;
84
86
dianaPrince->superpower = "Lasso of Truth";
87
+
85
88
// Using the -> operator to access member variable superpower:
86
89
std::cout << "Wonder Woman, possesses the mighty " << dianaPrince->superpower;
90
+
87
91
// Memory cleanup:
88
92
delete dianaPrince;
89
93
```
@@ -127,6 +131,5 @@ It is your responsibility to detect these cases and ensure those pointers are su
127
131
In older code, you might encounter two alternatives to `nullptr`.
128
132
Firstly, the literal `0` is specifically interpreted as a null value for pointers, though it's the only scenario where an integral literal can be assigned to a pointer.
129
133
Secondly, the `preprocessor macro` `NULL`, inherited from C and defined in the `<cstddef>` header, is another representation of a null pointer, though its usage is less common in modern C++ code.
@@ -87,19 +76,9 @@ In this task, you will implement the `uv_alarm` function to determine whether an
87
76
The `uv_alarm` function should use the provided `uv_light_heuristic` function, which operates on a vector of data and returns a value based on certain thresholds.
88
77
This is a mockup version of the complex code that will run during production, please don't change the interface.
89
78
90
-
### Task
91
-
92
79
Define the `uv_alarm` function in the `speedywagon` namespace. It should:
93
80
94
81
- Take a pointer to a `pillar_men_sensor` _struct_ as its parameter.
95
82
- Return `false` if the sensor pointer is null.
96
83
- Call the `uv_light_heuristic` function, passing the address of the sensor's `data` array.
97
84
- Return `true` if the value returned by `uv_light_heuristic` is greater than the `sensor->activity` level, otherwise return `false`.
98
-
99
-
## Wrapping Up
100
-
101
-
You’ve been entrusted with an essential task for the Speedywagon Foundation.
102
-
By testing for valid sensor connections, counting activity, and implementing alarm controls, you’ve ensured that the Foundation's battle against the Pillar Men can continue uninterrupted.
103
-
104
-
As a modern C++ engineer, you’d prefer using smart pointers, but alas, legacy code demands respect for the old ways.
105
-
The fate of humanity may rest on these pointers, so proceed carefully, and may the Hamon energy guide you.
0 commit comments