Commit 99684612 authored by Alexander Hirsch's avatar Alexander Hirsch

Add ex02

parent cbbab269
# Advanced C++ Programming
| Date | Topic / Exercise |
| ---------- | -------------------------- |
| 2019-10-01 | Welcome + [Crap] C Session |
| 2019-10-08 | [Crap] Tools Session |
| 2019-10-15 | [Crap] Git Session |
| 2019-10-22 | [Crap] Scripting Session |
| 2019-10-29 | [Crap] Techniques Session |
| 2019-11-05 | [Infrastructure] |
| 2019-11-12 | |
| 2019-11-19 | |
| 2019-11-26 | |
| 2019-12-03 | |
| 2019-12-10 | |
| 2020-01-07 | |
| 2020-01-14 | |
| 2020-01-21 | |
| 2020-01-28 | |
| Date | Topic / Exercise |
| ---------- | --------------------------------- |
| 2019-10-01 | Welcome + [Crap] C Session |
| 2019-10-08 | [Crap] Tools Session |
| 2019-10-15 | [Crap] Git Session |
| 2019-10-22 | [Crap] Scripting Session |
| 2019-10-29 | [Crap] Techniques Session |
| 2019-11-05 | [Infrastructure] |
| 2019-11-12 | [New Tools in a Familiar Context] |
| 2019-11-19 | |
| 2019-11-26 | |
| 2019-12-03 | |
| 2019-12-10 | |
| 2020-01-07 | |
| 2020-01-14 | |
| 2020-01-21 | |
| 2020-01-28 | |
[Crap]: https://git.uibk.ac.at/c7031162/crap
[Infrastructure]: ex01
[New Tools in a Familiar Context]: ex02
The lab takes place in [rr15](https://www.uibk.ac.at/zid/raeume/architekturgebaeude/rr15.html).
......
# Exercise Sheet 2 — New Tools in a Familiar Context
*due on 12 November 2019*
## Task 1 (4 Points)
This task focuses on the correct implementation of RAII as well as copy and move semantics.
You are asked to implement the concept of `unique_ptr` and `shared_ptr`.
Since we won't concern ourselves with templates for the moment your implementation will *own* an instance of the following `struct`.
```cpp
struct Vec2 {
int x, y;
};
```
- read the documentation regarding *smart pointers*, `unique_ptr`, and `shared_ptr`
- implement your version of `unique_ptr_to_vec2` and `shared_ptr_to_vec2` fulfilling these requirements:
- *dynamically* allocate an instance of `Vec2` in your constructor (initialise `x` and `y` to `0`)
- deallocate the `Vec2` instance in your destructor
- implement correct copy semantics (copy constructor / copy assignment)
- implement correct move semantics (move constructor / move assignment)
- enable access to `Vec2` via the operators `*` and `->`
- thread-safety for `shared_ptr_to_vec2`'s reference counter is not required
- pay attention to corner-cases like self-assignment (`v = v`)
- prepare a few interesting test cases for your presentation
- check your implementation for memory leaks and memory corruption using `valgrind`
## Task 2 (2 Points)
Examine the program `iterations.cpp` and think about the expected output.
Compile the program and run it.
What do you notice?
Did you expect this behaviour?
Did you get any compiler warnings?
Investigate what is actually happening (consider using `valgrind` or a debugger).
How can such errors be prevented?
Look for tools (ex: static code analysers) which help discovering such faulty code.
**Note:** If you run the executable and everything seems normal, try changing the initial content of `xs`, using different optimisation flags, or a different compiler.
The actual behaviour of this executable depends on various factors.
## Task 3 (2 Points)
You are given the program `strange.cpp`.
Compile it with different compilers and optimisation flags.
What do you notice?
What is really happening here?
## Additional Resources
- 🎥 [CppCon 2015 – *Give me 15 minutes & I'll change your view of GDB*](https://www.youtube.com/watch?v=PorfLSr3DDI)
- [Iterator Invalidation](https://en.cppreference.com/w/cpp/container#Iterator_invalidation)
- [Undefined Behaviour](https://en.cppreference.com/w/cpp/language/ub)
- [Rule of Three](https://en.wikipedia.org/wiki/Rule_of_three_(C%2B%2B_programming))
- [Defining the undefinedness of C](https://dl.acm.org/citation.cfm?id=2737979)
- [It's Time To Do CMake Right](https://pabloariasal.github.io/2018/02/19/its-time-to-do-cmake-right)
#include <iostream>
#include <vector>
int main()
{
std::vector<int> xs{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
for (auto it = xs.begin(); it != xs.end(); ++it) {
for (int i = 0; i < *it; i++) {
xs.push_back(*it);
}
}
std::cout << "Vector: ";
for (const auto &x : xs) {
std::cout << x << " ";
}
std::cout << "\n";
}
#include <iostream>
int array[5] = {};
bool contains(int v)
{
for (int i = 0; i <= 5; ++i) {
if (array[i] == v) {
return true;
}
}
return false;
}
int main()
{
for (int i = 0; i < 10; ++i) {
auto contains_text = contains(i) ? "contains " : "does not contain ";
std::cout << "array " << contains_text << i << std::endl;
}
std::cout << "done" << std::endl;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment