WEBVTT

00:00.390 --> 00:03.990
Hello again! In this video, we are going to talk about resource management.

00:04.350 --> 00:07.020
This is a very important topic in C++.

00:07.380 --> 00:11.220
We have mentioned it before, but I want to spend a bit more time talking about it.

00:15.840 --> 00:21.810
When we write programs, we often use external resources: resources from the operating system or third

00:21.810 --> 00:22.590
party libraries.

00:23.160 --> 00:30.660
So things like heap memory when we call new to allocate memory on the heap; files, database connections,

00:31.080 --> 00:35.520
windows in a GUI system, threads in a multi-threaded program and so on.

00:35.910 --> 00:37.920
And we need to manage these resources.

00:38.520 --> 00:42.250
Usually, we have to get the resource somehow before we can use it.

00:42.270 --> 00:48.000
We need to allocate memory, open a file, acquire a connection to the database and so on.

00:49.320 --> 00:52.680
Once we have finished using the resource, we need to release it or close it.

00:53.790 --> 00:59.760
If we need to make a copy of something - which often happens in C++, because of value semantics -

01:00.210 --> 01:02.460
we usually need to think about what happens then.

01:02.970 --> 01:05.580
And we also need to think about what happens if there is an error.

01:10.370 --> 01:15.830
There are some classes in the standard library which manage resources. We have already met string and vector,

01:15.830 --> 01:19.460
which manage allocated memory. fstream manages files.

01:19.940 --> 01:22.670
And there are some others, like the objects used in multi-threading.

01:23.120 --> 01:27.170
And by the way, if you're interested in learning about multi-threading, I have a course on that.

01:29.910 --> 01:36.000
These all follow the object oriented principle of encapsulation. The actual details of how to manage

01:36.000 --> 01:39.330
and interact with the resource are hidden away inside the class.

01:40.170 --> 01:44.970
If you are writing code that uses the class, it does not need to know anything about how the class

01:44.970 --> 01:46.230
works or what it is doing.

01:46.860 --> 01:51.810
You just go through the class's interface, and that usually means calling the public member functions

01:51.810 --> 01:52.470
of the class.

01:56.840 --> 02:02.510
As an example of this, the fstream class encapsulates the details of how to interact with a file.

02:03.260 --> 02:05.240
If we want to use a file in C++,

02:05.240 --> 02:07.330
we just create an fstream object.

02:07.340 --> 02:10.310
We give the name of the file as the argument to the constructor.

02:10.910 --> 02:16.310
We use the overloaded left and right shift operators, depending on whether we are doing input or output.

02:16.880 --> 02:22.220
And when we have finished, we close the file. Or in fact, we can forget about that, and the stream object

02:22.220 --> 02:24.320
will close the file when it goes out of scope.

02:26.690 --> 02:30.860
So to actually access the data in the file, we just use the fstream interface.

02:31.280 --> 02:34.550
We do not need to know anything about how to work with files directly.

02:34.940 --> 02:39.600
The peculiarities of files on Windows or Linux, or whatever system we are using.

02:44.870 --> 02:52.220
These classes in the C++ library follow a common idiom: the resource or a handle to the resource is stored

02:52.220 --> 02:54.350
as a private data member of the class.

02:55.190 --> 02:58.850
The class constructor will acquire or open the resource.

02:59.900 --> 03:05.330
The class will have public member functions which control access to the resource, and then the destructor

03:05.330 --> 03:07.400
for the class will release the resource.

03:09.170 --> 03:13.190
And there is in fact a name for this idiom, which is absolutely terrible!

03:14.600 --> 03:18.890
Resource Acquisition Is Initialization. RAII for short.

03:20.030 --> 03:27.110
What this means is that when you create a class object, that will acquire a copy of the resource and

03:27.110 --> 03:33.500
manage it. And, by implication, when the class is destroyed, it will make sure that the resource is

03:33.500 --> 03:34.400
properly released.

03:36.960 --> 03:42.020
The other important thing is that, when an object is copied or assigned to, so C++ does a lot of copying

03:42.020 --> 03:42.560
objects.

03:44.300 --> 03:48.950
When that happens, the target object, the one which is the result of the operation, will have its

03:48.950 --> 03:50.420
own version of the resource.

03:50.750 --> 03:55.070
So you will not have problems where different objects are writing to the same file, scrambling the

03:55.070 --> 03:57.140
output or writing over each other's memory.

04:01.570 --> 04:08.560
The advantages of this approach, RAII: obtaining access to the resource is straightforward and deterministic,

04:09.520 --> 04:15.160
if you can access the resource, then you have a valid object which has been successfully created.

04:16.570 --> 04:21.220
If you do not get a valid object, then you will know that the resource cannot be used by the program.

04:22.360 --> 04:24.130
Using the resource is straightforward.

04:24.130 --> 04:26.500
You just call the member functions on the object.

04:28.090 --> 04:30.400
And releasing it is straightforward and deterministic.

04:31.000 --> 04:34.090
The resource will be released when the object goes out of scope.

04:34.840 --> 04:40.660
So that means either when the program reaches the end of the scope where the object was created or if

04:40.660 --> 04:44.440
an exception is thrown. And we will talk more about exceptions later on.

04:45.070 --> 04:50.980
Another teaser! And also only one object can own a given resource at one time.

04:51.490 --> 04:55.240
So there is no possibility of different objects interfering with each other.

04:57.810 --> 04:59.280
OK, so that is it for this feature.

04:59.640 --> 05:02.460
I will see you next time, but meanwhile, keep coding!
