Skip to content

Commit acc3255

Browse files
authored
fix: improve docs to align with tests (#925)
* fix: improve docs to align with tests * tests: fix closing bracket.
1 parent 0063583 commit acc3255

File tree

4 files changed

+34
-54
lines changed

4 files changed

+34
-54
lines changed

Diff for: concepts/pointers/about.md

+12-9
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ With modern C++ there are also _smart pointers_, the basic type is not smart at
1313

1414
Before digging into the details, it's worth understanding the use of _pointers_.
1515
_Pointers_ are a way to share an object's address with other parts of our program, which is useful for two major reasons:
16+
1617
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.
1920

2021
## General Syntax
2122

@@ -48,10 +49,10 @@ _Pointer arithmetic_ allows you to perform arithmetic operations on pointers, wh
4849
Adding an integer to a pointer makes it point to a different element.
4950

5051
```cpp
51-
// Stargate addresses
52-
int gateAddresses[] = {462, 753, 218, 611, 977};
53-
// 'ptr' points to the first element of 'gateAddresses'
54-
int* ptr{gateAddresses};
52+
// Stargate Coordinate Code
53+
int gateCode[] = {462, 753, 218, 611, 977};
54+
// 'ptr' points to the first element of 'gateCode'
55+
int* ptr{&gateCode[0]};
5556
// Accesses the third Stargate address through pointer arithmetic
5657
int dialedAddress{*(ptr + 2)};
5758
// Chevron encoded! Dialing Stargate address:
@@ -80,10 +81,13 @@ struct Superhero {
8081
std::string superpower;
8182
};
8283
83-
Superhero* dianaPrince = new Superhero;
84+
Superhero wonder_woman{};
85+
Superhero* dianaPrince = &wonder_woman;
8486
dianaPrince->superpower = "Lasso of Truth";
87+
8588
// Using the -> operator to access member variable superpower:
8689
std::cout << "Wonder Woman, possesses the mighty " << dianaPrince->superpower;
90+
8791
// Memory cleanup:
8892
delete dianaPrince;
8993
```
@@ -127,6 +131,5 @@ It is your responsibility to detect these cases and ensure those pointers are su
127131
In older code, you might encounter two alternatives to `nullptr`.
128132
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.
129133
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.
130-
~~~~
131-
132134
[ariane-flight-v88]: https://en.wikipedia.org/wiki/Ariane_flight_V88
135+
~~~~

Diff for: concepts/pointers/introduction.md

+9-10
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ With modern C++ there are also _smart pointers_, the basic type is not smart at
1313

1414
Before digging into the details, it's worth understanding the use of _pointers_.
1515
_Pointers_ are a way to share an object's address with other parts of our program, which is useful for two major reasons:
16+
1617
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.
1920

2021
## General Syntax
2122

@@ -48,10 +49,10 @@ _Pointer arithmetic_ allows you to perform arithmetic operations on pointers, wh
4849
Adding an integer to a pointer makes it point to a different element.
4950

5051
```cpp
51-
// Stargate addresses
52-
int gateAddresses[] = {462, 753, 218, 611, 977};
53-
// 'ptr' points to the first element of 'gateAddresses'
54-
int* ptr{gateAddresses};
52+
// Stargate Coordinate Code
53+
int gateCode[] = {462, 753, 218, 611, 977};
54+
// 'ptr' points to the first element of 'gateCode'
55+
int* ptr{&gateCode[0]};
5556
// Accesses the third Stargate address through pointer arithmetic
5657
int dialedAddress{*(ptr + 2)};
5758
// Chevron encoded! Dialing Stargate address:
@@ -79,8 +80,8 @@ The `->` operator is used to access the member variable `superpower`, showcasing
7980
struct Superhero {
8081
std::string superpower;
8182
};
82-
83-
Superhero* dianaPrince = new Superhero;
83+
Superhero wonder_woman{};
84+
Superhero* dianaPrince = &wonder_woman;
8485
dianaPrince->superpower = "Lasso of Truth";
8586
// Using the -> operator to access member variable superpower:
8687
std::cout << "Wonder Woman, possesses the mighty " << dianaPrince->superpower;
@@ -95,5 +96,3 @@ Pointers offer the flexibility of changing their target object and can be assign
9596
However, this flexibility introduces risks, such as dereferencing null pointers or creating dangling pointers.
9697
References, on the other hand, cannot be null and are bound to valid objects upon creation, avoiding these risks.
9798
Given their safer nature, references should be preferred over pointers unless the additional functionalities provided by pointers are necessary.
98-
99-
[ariane-flight-v88]: https://en.wikipedia.org/wiki/Ariane_flight_V88

Diff for: exercises/concept/speedywagon/.docs/instructions.md

+4-25
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ However, in recent times, the sensors that track Pillar Men activities are malfu
88
The Foundation's systems are old, and the code interacts with a legacy C++ library that cannot be updated.
99
Your task is to implement four core functions that monitor Pillar Men sensor activity using an old-fashioned pointer-based library.
1010

11-
The Foundation's operations rely on you.
12-
13-
## 0. The Sensor Environment (`pillar_men_sensor`)
11+
As a modern C++ engineer, you’d prefer using smart pointers, but alas, legacy code demands respect for the old ways.
12+
The fate of humanity may rest on these pointers, so proceed carefully, and may the Hamon energy guide you.
1413

14+
~~~~exercism/note
1515
As sensor readings can be huge, we supply a mockup _struct_ that is used in the actual library.
1616
The code has already been implemented in the header file for you.
1717
@@ -22,20 +22,17 @@ struct pillar_men_sensor {
2222
std::vector<int> data{};
2323
};
2424
```
25+
~~~~
2526

2627
## 1. Check Sensor Connection (`connection_check`)
2728

2829
Your first task is to ensure that the Pillar Men sensor is connected properly.
2930
We can't have false alarms triggered by disconnected sensors.
3031
You will write a function `connection_check`, which tests if the sensor's pointer is valid by checking for `nullptr`.
3132

32-
### Task
33-
3433
- Define a function that accepts a pointer a a `pillar_men_sensor` _struct_.
3534
- The function should return `true` if the sensor pointer is not null, and `false` otherwise.
3635

37-
### Example
38-
3936
```cpp
4037
pillar_men_sensor* sensor{nullptr};
4138
bool isConnected = connection_check(sensor);
@@ -47,14 +44,10 @@ bool isConnected = connection_check(sensor);
4744
Pillar Men are lurking in the shadows, and we need to know if sensors have detected any activity.
4845
You will write the `activity_counter` function, which takes in an array of sensors and a capacity indicating the number of sensors in the array.
4946
50-
### Task
51-
5247
- Define a function that accepts a pointer to the first element of an array and the arrays capacity.
5348
- Use pointer arithmetic to loop through the sensor array and accumulate the activity readings.
5449
- Return the accumulated activity.
5550
56-
### Example
57-
5851
```cpp
5952
pillar_men_sensor sensor_array[3] = {{0}, {101}, {22}};
6053
int totalActivity = activity_counter(sensor_array, 3);
@@ -67,14 +60,10 @@ Not every sensor should trigger an alarm unless there’s real danger.
6760
The `alarm_control` function ensures that a sensor only triggers an alarm if its activity level is greater than 0.
6861
This function should also check for null sensors to prevent system crashes.
6962

70-
### Task
71-
7263
- Define a function that accepts the pointer to a `pillar_men_sensor`.
7364
- The function should first check for a `nullptr` sensor. If the sensor is `nullptr`, return `false`.
7465
- If the sensor is valid and its activity is greater than 0, return `true`; otherwise, return `false`.
7566

76-
### Example
77-
7867
```cpp
7968
pillar_men_sensor db{9008, "songokunoie", {7, 7, 7}};
8069
bool alarm = alarm_control(&db);
@@ -87,19 +76,9 @@ In this task, you will implement the `uv_alarm` function to determine whether an
8776
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.
8877
This is a mockup version of the complex code that will run during production, please don't change the interface.
8978
90-
### Task
91-
9279
Define the `uv_alarm` function in the `speedywagon` namespace. It should:
9380
9481
- Take a pointer to a `pillar_men_sensor` _struct_ as its parameter.
9582
- Return `false` if the sensor pointer is null.
9683
- Call the `uv_light_heuristic` function, passing the address of the sensor's `data` array.
9784
- 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.

Diff for: exercises/concept/speedywagon/.docs/introduction.md

+9-10
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ With modern C++ there are also _smart pointers_, the basic type is not smart at
1313

1414
Before digging into the details, it's worth understanding the use of _pointers_.
1515
_Pointers_ are a way to share an object's address with other parts of our program, which is useful for two major reasons:
16+
1617
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.
1920

2021
## General Syntax
2122

@@ -48,10 +49,10 @@ _Pointer arithmetic_ allows you to perform arithmetic operations on pointers, wh
4849
Adding an integer to a pointer makes it point to a different element.
4950

5051
```cpp
51-
// Stargate addresses
52-
int gateAddresses[] = {462, 753, 218, 611, 977};
53-
// 'ptr' points to the first element of 'gateAddresses'
54-
int* ptr{gateAddresses};
52+
// Stargate Coordinate Code
53+
int gateCode[] = {462, 753, 218, 611, 977};
54+
// 'ptr' points to the first element of 'gateCode'
55+
int* ptr{&gateCode[0]};
5556
// Accesses the third Stargate address through pointer arithmetic
5657
int dialedAddress{*(ptr + 2)};
5758
// Chevron encoded! Dialing Stargate address:
@@ -79,8 +80,8 @@ The `->` operator is used to access the member variable `superpower`, showcasing
7980
struct Superhero {
8081
std::string superpower;
8182
};
82-
83-
Superhero* dianaPrince = new Superhero;
83+
Superhero wonder_woman{};
84+
Superhero* dianaPrince = &wonder_woman;
8485
dianaPrince->superpower = "Lasso of Truth";
8586
// Using the -> operator to access member variable superpower:
8687
std::cout << "Wonder Woman, possesses the mighty " << dianaPrince->superpower;
@@ -95,5 +96,3 @@ Pointers offer the flexibility of changing their target object and can be assign
9596
However, this flexibility introduces risks, such as dereferencing null pointers or creating dangling pointers.
9697
References, on the other hand, cannot be null and are bound to valid objects upon creation, avoiding these risks.
9798
Given their safer nature, references should be preferred over pointers unless the additional functionalities provided by pointers are necessary.
98-
99-
[ariane-flight-v88]: https://en.wikipedia.org/wiki/Ariane_flight_V88

0 commit comments

Comments
 (0)