WEBVTT

00:00:00.940 --> 00:00:05.380
It's time for the summary of the final module in the course. We first

00:00:05.380 --> 00:00:09.310
talked about standard array class and how it can be used instead of a

00:00:09.310 --> 00:00:13.660
legacy raw array. Standard array is a templated class.

00:00:13.670 --> 00:00:17.160
The template arguments contain the data type of the elements

00:00:17.160 --> 00:00:19.550
and the number of elements in the array.

00:00:19.840 --> 00:00:21.810
Since the size is a template argument,

00:00:21.810 --> 00:00:24.780
we don't need to allocate a special place in memory to store

00:00:24.780 --> 00:00:28.710
it, so the total memory allocated for the standard array is

00:00:28.710 --> 00:00:31.560
just the total size of all elements.

00:00:32.340 --> 00:00:35.730
Standard array also has an at function which works

00:00:35.730 --> 00:00:37.990
just like a square brackets operator,

00:00:38.000 --> 00:00:42.520
but it also does bound checking. If you want to convert a

00:00:42.520 --> 00:00:46.960
raw array to the standard array, C++20 offers you a new

00:00:46.960 --> 00:00:48.860
to_array function to do that.

00:00:49.940 --> 00:00:52.270
We can create a templated function to pass the

00:00:52.270 --> 00:00:54.920
standard array object as an argument.

00:00:54.930 --> 00:00:57.510
That way, the function will be able to receive an

00:00:57.510 --> 00:01:00.460
array of any data type and size.

00:01:01.240 --> 00:01:05.690
Standard array also offers us some perks like making a copy of all

00:01:05.690 --> 00:01:10.730
elements by simply using the copy assignment operator. Iterators

00:01:10.730 --> 00:01:14.640
serve as an interface between a container object and a function

00:01:14.640 --> 00:01:16.620
from the algorithm library.

00:01:16.630 --> 00:01:22.250
We can pass the standard array's iterators to a function like sort to sort

00:01:22.260 --> 00:01:27.060
all of the elements by value, and we can also use range‑based for loops,

00:01:27.060 --> 00:01:30.260
because they utilize iterators in the background.

00:01:31.140 --> 00:01:33.940
C++20 also provides ranges.

00:01:33.950 --> 00:01:40.290
Ranges are an abstraction on top of iterators. Constrained algorithms from

00:01:40.290 --> 00:01:44.760
the range's namespace can be simpler to use, because all we need to do is to

00:01:44.760 --> 00:01:51.100
pass the container itself, we don't need to use iterators explicitly. Ranges

00:01:51.100 --> 00:01:55.350
library also offers us the ability to create a chain of views to work with

00:01:55.350 --> 00:02:00.520
temporary ranges and form a new perspective on the existing sequential data

00:02:00.520 --> 00:02:01.360
structures.

00:02:02.340 --> 00:02:04.370
If you need dynamic arrays on the heap,

00:02:04.380 --> 00:02:08.550
you can use the vector class from the standard library. Vectors can

00:02:08.550 --> 00:02:11.680
also be resized, so if you want to add more elements,

00:02:11.680 --> 00:02:15.370
vectors will allocate a larger internal array on the heap,

00:02:15.380 --> 00:02:18.710
and move all of the elements from the old array to keep

00:02:18.710 --> 00:02:20.850
them all in one block of memory.

00:02:21.740 --> 00:02:25.050
If you don't know the exact size of the vector at declaration,

00:02:25.060 --> 00:02:27.770
you can use the push_back function to push another

00:02:27.770 --> 00:02:30.650
element to the end of the internal array.

00:02:30.680 --> 00:02:33.260
This will resize the vector automatically,

00:02:33.640 --> 00:02:38.750
but resizing is computationally expensive, so to keep it to minimum,

00:02:38.750 --> 00:02:42.060
we can reserve capacity with the reserve function.

00:02:42.740 --> 00:02:47.120
We can also insert or erase elements at a specific position in the

00:02:47.120 --> 00:02:51.160
vector by using the insert or erase functions.

00:02:52.040 --> 00:02:54.730
If you need to remove a specific value from the vector,

00:02:54.730 --> 00:02:58.520
you can move all of the elements with that value to the end of the vector

00:02:58.520 --> 00:03:03.990
by using the remove function. Then, you can simply delete those elements

00:03:03.990 --> 00:03:08.440
with erase, but there is also a standard erase function which combines

00:03:08.440 --> 00:03:11.860
remove and erase in one single function.

00:03:12.740 --> 00:03:16.380
The main idea around the smart pointer is that it has an

00:03:16.390 --> 00:03:18.850
ownership of the pointed resource.

00:03:18.860 --> 00:03:23.990
That means that a smart pointer is an RAII class, which wraps around the

00:03:23.990 --> 00:03:27.900
raw pointer, so when an object of this class is deleted,

00:03:27.910 --> 00:03:31.550
destructor will also deallocate the owned resource.

00:03:32.340 --> 00:03:36.600
Standard library offers us an exclusive ownership of the resource

00:03:36.600 --> 00:03:41.670
with unique pointers. Unique pointers cannot be cloned, they can only

00:03:41.670 --> 00:03:44.460
transfer ownership with move semantics.

00:03:45.340 --> 00:03:48.280
Shared pointers don't have exclusive ownership,

00:03:48.290 --> 00:03:52.150
they co‑own resources with other shared pointers.

00:03:52.540 --> 00:03:55.920
The amount of shared pointers which point to the same resource is kept

00:03:55.920 --> 00:03:58.960
inside of a dynamically allocated control block.

00:03:59.340 --> 00:04:02.040
Once the last one of these pointers is deleted,

00:04:02.050 --> 00:04:07.090
destructor will deallocate the owned resource. Weak pointers

00:04:07.090 --> 00:04:10.540
have temporary ownership, they can only point to the resources

00:04:10.550 --> 00:04:12.900
owned by other shared pointers.

00:04:13.120 --> 00:04:16.760
When we want to use them to access the pointed resource, we have to use the

00:04:16.760 --> 00:04:20.160
lock function to turn them into a shared pointer first.

00:04:21.040 --> 00:04:24.730
And, that's it, we are finally at the end of the whole course.

00:04:24.740 --> 00:04:28.070
I hope that you gained a lot of new insight about the world of

00:04:28.070 --> 00:04:30.430
pointers and memory management.

00:04:30.440 --> 00:04:34.250
Don't hesitate to join the discussion if you have any questions.

00:04:34.260 --> 00:04:38.050
Thank you for watching, and good luck on your future endeavors.
