Low-Level Programming: Post 6 29/10/15
October 29th 2015 is the date of the sixth lesson on Low-Level Programming.
At the start of the lesson, we were given, as usual, an overview of what we were going to learn today. The following bullet points are the list of features that were taught in the lesson (including a guide as to how to make the practical from last week work and how it should have been done)...
- Libs: Last week's practical reviewed.
- Container: Vectors, Arrays, Linked Lists and Deques i.e. non-associative.
- Iterators: What they are and how they work.
- Erase-Remove Idiom: How to search and erase elements in containers provided by the STL.
Before we began working on the bullet points above, there we three 'important pieces of information' given to us.
The first 'piece of information' was on how the lessons were going to be covered from next week onwards. It said that in the three hour lesson(s), one hour will go into the concepts (lecture) and the other two will go into the practicals. We were also told/recommended to go and work/practice on any pieces of work that were provided to us over the past few weeks, since there isn't enough time to cover everything in extensive detail.
The second 'piece of information' was essentially a recap on Shared_Ptrs, Weak_Ptrs, Casting and Implicit Casting. Since we have covered the above points in the past two posts, I won't be going into extensive detail again on them.
The third, and unquestionably the most important 'piece of information' was the assignment brief given to us. It covered the marking scheme and expectations for the first part of the assignment. It goes as follows...
The third, and unquestionably the most important 'piece of information' was the assignment brief given to us. It covered the marking scheme and expectations for the first part of the assignment. It goes as follows...
- 75% Portfolio: Portfolio of Tasks, Assignments and Practical Work.
- 40& Exercise One: 40% of the 75% for this one, with the instructions on Blackboard.
The next part covered the deliverables necessary for the assignment...
- A Single Executable Running.
- A Menu System.
- And the Three Games Required.
- The Three Games listed above are Blackjack, Hangman and Snake.
The most crucial part of the brief was perhaps the breakdown of the assignment...
- 10% for the Coding Style.
- 10 % for the Menu System: Console Based, the System should be the first point of entry into your game.
- 15% for the Object-Oriented Programming.
- 15% for Blackjack: Implementing the game and meeting the grading criteria. This game shouldn't pose too many problems as it's rules are simple.
- 20% for Hangman: Should be trickier than Blackjack but still achievable.
- 30% for Snake: The trickiest game of the three, hence it carries more marks. At least attempts at marks are awarded for coding design decisions.
The last part of the brief was a general guidance on how to work on the assignment...
- Console: Don't focus on graphics; this assignment is all about code design and OPP decision making. I.e. Correct usage of inheritance and memory.
- Try: Do try to complete all three game. Even if the game isn't fully working, there are marks for the design of the games.
- Easy Marks: Just following the Coding Style gets you 10%, not to mention the Menu System another 10%.
- Tutorials: Whilst the tutorials won't tell you exactly how to resolve all the gameplay mechanics, you will get the chance to work with the elements needed to complete the task.
- Attendance: Skipping lectures and tutorials will do you no favours in the long run. It's the opportunity to practice the concepts.
After the assignment brief, we were given a guide as to how the practical from last week's lesson should have worked. I managed to get most of the instruction guides. They are as follows...
- Create a New Project.
- Select Console Application.
- Do NOT include two 'additional options'.
- Open File in File Explorer.
- Copy 'Libcaca' (name of project) into project.
- #include 'caca.h' -> Error.
- Go into Project Properties.
- Additional, include dependencies/directions.
- #include 'caca.h'
- #include <iostream>
- Go into Project->Linker->All Options.
- Additional Library Directions.
- X86 -> Debug.
- Full Stop, not Comma.
- Add Void ShowMessage(); before Main.
- Add ShowMessage(); IN Main.
Now that the parts before on what we were going to learn finished, we started on learning on the bullet point features. As it always is, I will not be going over past features (in this case, a basic Vectors recap).
Vector: Common Operations
- Iterators:
- begin & cbegin - Iterator to First Element.
- end & cend - Iterator to Last Element.
- rbegin & crbegin - Reverse Iterator to First Element.
- rend & crend - Reverse Iterator to Last Element.
Vector: Reverse Iterator
- Reverse Iterator: Reverse Iterators traverse backwards through the array. And they also support const.
Vector: Capacity
- Size: We can find out the size of the array.
- Capacity: We can check the capacity of the array.
- Reserve: We can reserve a block of memory for X objects.
Vector: In Summary
- Arrays: A good alternative to traditional arrays in C and C++.
- Performance: Shows efficient and fast performance when iterating through the array.
- Memory: Contiguous memory usage benefits caching and prevents fragmentation.
- Memory Allocation: Dynamic reallocation is powerful but also slow, so reserve a reasonable amount of space up front.
- Iterators: Provides iterators and individual access using [] operator.
Iterators
- What Are They?: An Iterator is any object that points to an element in a container and has the ability to iterate through the container's range.
- Operation: Usage of the increment (++), decrement (--) and deference (*) operations.
- Pointer: A raw pointer can be thought as an iterator, due to it's ability to increment and decrement i.e. pointer arithmetic.
- Behaviour: A pointer is a type of iterator, but not all iterators work the same way as pointers.
- Types:
- Input.
- Output.
- Forward.
- Bidirectional.
- Random Access.
Erase-Remove Idiom
- What is it?: A C++ technique to remove container elements that fulfill certain conditions.
- Loops: It's possible to achieve the same behaviour by using loops to locate elements to erase. but this is considered simpler.
- C++ Std: Provides us with two algorithms that can be used to remove elements, Remove and remove_if.
- Iterators: They use iterators to determine the range that should be erased (2x forward iterators).
- Restructures: In order to ascertain the begin and end iterators, the container has to be re-arranged.
- Erase: The two remove algorithms do not erase the elements, the result is normally passed to an erase function.
Erase-Remove Idiom: Performance
- Removal: As remove and remove_if do not actually erase the items, but does move valid data, there must be some sort of overhead.
- Operation: In modern C++ the elements that are valid are moved to the front of the container. However, the elements to be deleted are not, this means they have an undefined but valid state.
- Distance: Performance of the algorithm is determined by the number of elements in it, though iteration (linear) should be a constant, the moving of the data is unknown.
- Erase: Don't forget to erase the results of remove function as the end of the array could be invalid. The erase-remove idiom comes from having to remove first before erasing.
Predicates
- Unary Predicates: STL algorithms often use predicates to accomplish tasks; such as remove.
- Return: Simply unary predicates are functions that take a single parameter and return a Boolean.
- C++ Std:We've seen already that remove_if utilises predicates to determine whether the element should be moved.
- Iterators: The predicate should take an iterator and deference it for usage within the function.
- Constant: The dereferenced object should not have any non-constant function called and must not invalidate iterators that the algorithm uses.
- Index Agnostic: The predicate should also not care about the order of elements tested or their position.
Std::Array
- Fixed Size Array: STL container that encapsulates fixed size arrays.
- Functionality: Designed to allow the simplicity and of an array but with support for the std algorithms and iterators.
- Performance: Has the same performance as a C style array.
Linked Lists
- Nodes: Linked Lists consist of nodes that link to each other. Forward lists utilise a next node ptr, to traverse both ways you need a prev node ptr.
- Disparate: The memory is allocated on a node by node basis. No guarantee is made to their location, thus potentially causing cache misses.
- Agnostic: As long as a node has a ptr to the next item in the list, the items they point to can be of any type.
- Removal: Removal of nodes causes a deallocation and then an update of the prev node, thus preventing wasted memory space.
- Dynamic Allocation: Prevents the overheads inherent in resizing arrays dynamically, i.e. allocation and then moves from one location to another.
- Constraints: You don't need random access to the elements and memory allocation/deallocation is more vital than cache misses.
Linked Lists: In Operation
- Adding/Removing: You can break the chain and add or remove new items easily.
- Performance: No overhead other than an allocation or deallocation when working with nodes.
Deque
- Double Ended: Sequence containers that can be expanded or contracted on both ends (front or back).
- Functionality: In terms of functionality, they work in a similar fashion to vectors, but they allow manipulation of items at both ends.
- Adding/Removing: They are efficient when removing or adding elements to either end of the array.
- Expansion: Like vectors, they can expand or contract and this is performed dynamically.
- Memory: Vectors guarantee contiguous blocks of memory but deques do not.
- Performance: They can grow more efficiently than vectors and are suited to tasks with lots of insertions or removals.
Deque: Workings
- Vector vs Deque: Vectors are contiguously allocated in memory, deques do not operate in the same manner.
When the lectural side of the lesson, along with the break finished, we had to work on the DungeonDeath game once again. Since I was working alone this weak, I was not making a lot of progress. I did manage to, however, add some more rooms to the level and fix the exit game option, since it left a 'This room is blocked' comment when closing the program down.


