It's time for the summary of the final module in the course. We first
talked about standard array class and how it can be used instead of a
legacy raw array. Standard array is a templated class.
The template arguments contain the data type of the elements
and the number of elements in the array.
Since the size is a template argument,
we don't need to allocate a special place in memory to store
it, so the total memory allocated for the standard array is
just the total size of all elements.
Standard array also has an at function which works
just like a square brackets operator,
but it also does bound checking. If you want to convert a
raw array to the standard array, C++20 offers you a new
to_array function to do that.
We can create a templated function to pass the
standard array object as an argument.
That way, the function will be able to receive an
array of any data type and size.
Standard array also offers us some perks like making a copy of all
elements by simply using the copy assignment operator. Iterators
serve as an interface between a container object and a function
from the algorithm library.
We can pass the standard array's iterators to a function like sort to sort
all of the elements by value, and we can also use range‑based for loops,
because they utilize iterators in the background.
C++20 also provides ranges.
Ranges are an abstraction on top of iterators. Constrained algorithms from
the range's namespace can be simpler to use, because all we need to do is to
pass the container itself, we don't need to use iterators explicitly. Ranges
library also offers us the ability to create a chain of views to work with
temporary ranges and form a new perspective on the existing sequential data
structures.
If you need dynamic arrays on the heap,
you can use the vector class from the standard library. Vectors can
also be resized, so if you want to add more elements,
vectors will allocate a larger internal array on the heap,
and move all of the elements from the old array to keep
them all in one block of memory.
If you don't know the exact size of the vector at declaration,
you can use the push_back function to push another
element to the end of the internal array.
This will resize the vector automatically,
but resizing is computationally expensive, so to keep it to minimum,
we can reserve capacity with the reserve function.
We can also insert or erase elements at a specific position in the
vector by using the insert or erase functions.
If you need to remove a specific value from the vector,
you can move all of the elements with that value to the end of the vector
by using the remove function. Then, you can simply delete those elements
with erase, but there is also a standard erase function which combines
remove and erase in one single function.
The main idea around the smart pointer is that it has an
ownership of the pointed resource.
That means that a smart pointer is an RAII class, which wraps around the
raw pointer, so when an object of this class is deleted,
destructor will also deallocate the owned resource.
Standard library offers us an exclusive ownership of the resource
with unique pointers. Unique pointers cannot be cloned, they can only
transfer ownership with move semantics.
Shared pointers don't have exclusive ownership,
they co‑own resources with other shared pointers.
The amount of shared pointers which point to the same resource is kept
inside of a dynamically allocated control block.
Once the last one of these pointers is deleted,
destructor will deallocate the owned resource. Weak pointers
have temporary ownership, they can only point to the resources
owned by other shared pointers.
When we want to use them to access the pointed resource, we have to use the
lock function to turn them into a shared pointer first.
And, that's it, we are finally at the end of the whole course.
I hope that you gained a lot of new insight about the world of
pointers and memory management.
Don't hesitate to join the discussion if you have any questions.
Thank you for watching, and good luck on your future endeavors.