Hello again! In this video, we are going to talk about resource management.
This is a very important topic in C++.
We have mentioned it before, but I want to spend a bit more time talking about it.
When we write programs, we often use external resources: resources from the operating system or third
party libraries.
So things like heap memory when we call new to allocate memory on the heap; files, database connections,
windows in a GUI system, threads in a multi-threaded program and so on.
And we need to manage these resources.
Usually, we have to get the resource somehow before we can use it.
We need to allocate memory, open a file, acquire a connection to the database and so on.
Once we have finished using the resource, we need to release it or close it.
If we need to make a copy of something - which often happens in C++, because of value semantics -
we usually need to think about what happens then.
And we also need to think about what happens if there is an error.
There are some classes in the standard library which manage resources. We have already met string and vector,
which manage allocated memory. fstream manages files.
And there are some others, like the objects used in multi-threading.
And by the way, if you're interested in learning about multi-threading, I have a course on that.
These all follow the object oriented principle of encapsulation. The actual details of how to manage
and interact with the resource are hidden away inside the class.
If you are writing code that uses the class, it does not need to know anything about how the class
works or what it is doing.
You just go through the class's interface, and that usually means calling the public member functions
of the class.
As an example of this, the fstream class encapsulates the details of how to interact with a file.
If we want to use a file in C++,
we just create an fstream object.
We give the name of the file as the argument to the constructor.
We use the overloaded left and right shift operators, depending on whether we are doing input or output.
And when we have finished, we close the file. Or in fact, we can forget about that, and the stream object
will close the file when it goes out of scope.
So to actually access the data in the file, we just use the fstream interface.
We do not need to know anything about how to work with files directly.
The peculiarities of files on Windows or Linux, or whatever system we are using.
These classes in the C++ library follow a common idiom: the resource or a handle to the resource is stored
as a private data member of the class.
The class constructor will acquire or open the resource.
The class will have public member functions which control access to the resource, and then the destructor
for the class will release the resource.
And there is in fact a name for this idiom, which is absolutely terrible!
Resource Acquisition Is Initialization. RAII for short.
What this means is that when you create a class object, that will acquire a copy of the resource and
manage it. And, by implication, when the class is destroyed, it will make sure that the resource is
properly released.
The other important thing is that, when an object is copied or assigned to, so C++ does a lot of copying
objects.
When that happens, the target object, the one which is the result of the operation, will have its
own version of the resource.
So you will not have problems where different objects are writing to the same file, scrambling the
output or writing over each other's memory.
The advantages of this approach, RAII: obtaining access to the resource is straightforward and deterministic,
if you can access the resource, then you have a valid object which has been successfully created.
If you do not get a valid object, then you will know that the resource cannot be used by the program.
Using the resource is straightforward.
You just call the member functions on the object.
And releasing it is straightforward and deterministic.
The resource will be released when the object goes out of scope.
So that means either when the program reaches the end of the scope where the object was created or if
an exception is thrown. And we will talk more about exceptions later on.
Another teaser! And also only one object can own a given resource at one time.
So there is no possibility of different objects interfering with each other.
OK, so that is it for this feature.
I will see you next time, but meanwhile, keep coding!