C + + is known as a multi-paradigm general-purpose programming language, but Raii has actually become an indispensable core technology in C + + programming technology. Raii's almost ubiquitous presence comes not only from the strong advocacy of the father of C + +, but also from the simple, efficient and almost omnipotent adaptation of the technology itself.
If you haven't heard of RAII, then I'll recount it here, RAII is the initials of the following English phrases:
Resource acquisition is initialisation
The meaning of this sentence is: The acquisition of resources is initialized. This is just a phrase that cannot be relied on words too literally to understand the full meaning behind the literal, but the phrase itself does reflect an important argument: the resource is one, and initialization is the second.
RAII is concerned with resources. Resources are a limited amount of information that needs to be allocated. For example, memory, file handle, network socket port, database connection, and thread pool. Basically, due to physical limitations, all the information is limited. In some special cases, the information is greatly enriched by the local resources, such as sand, air and so on. But in most cases, the information is limited and requires our good management.
The most basic form of resource management is to finish the thing. The application of resources, run out, will be returned. In C + + programmer life is the most common memory resources, resource management is memory management: The application of memory, no matter when the logical completion of the use of this piece of memory, memory will be correctly released. Note that the word "no matter when" is used here. In practical applications, the use of memory logic is so complex that it is logical to define the life cycle of a block of memory will become a very cumbersome and complex task, and memory resources will be in the human intelligence leaks out. Even in simple cases, memory can slip away in the clumsy muddle of rookie programmers. Therefore, although the resource management can be simplified as a "finish", in practice it is difficult to be guaranteed.
There is a language, such as Java, that takes over memory resources, provides so-called automatic memory management, and simulates an inexhaustible quasi-infinite memory pattern for programmers using memory allocation algorithms. The idea behind this is that in a common application, memory usage is relatively centralized in both space and time, allowing for less memory to cope with the infinite memory requests that accumulate on time. Programmers use such languages without having to consider the release of memory. The burden is greatly reduced.
Automatic memory management from the principle of doubling memory resources to create a data (quasi) infinite virtual environment, so that the programmer freed from the heavy memory resource management, more energy to consider the actual transaction code, improve productivity. But it also has its own limitations. One, automatic memory management algorithm is more complex, its own program will occupy a certain amount of memory, while automatic memory management with time to change space, but also requires the actual physical memory at least to apply the maximum instantaneous memory required twice times to better play a role, this requirement that the automatic memory management is not in the sense of the lack of management "resources" , but provides a plausible alternative to the less wasteful use of rich information. Secondly, since automatic memory management is separated from specific applications and cannot know the most suitable pointcut, the intervention of automatic memory management is basically unpredictable. This limits the application of automatic memory management in those programs that are more stringent in response to time requirements. Finally, automatic memory management is only the management of memory resources and it cannot manage other resources. In addition to memory, programmers often have to deal with other resources. Automatic memory management mode cannot be applied to other types of resource management.
C + + provides RAII as a real resource management practical solution. This is also the contribution of the C + + language to the broader issue of resource management. Although its practical significance is so significant, its approach is simple, using classes to represent resources, allocating resources in class constructors, and releasing resources in the class's destructor. Like what
class Resource {
public:
Resource(const char *name) : _resource(alloc_resource(name)) {……}
~Resource() { release_resource (_resource); }
};
The use of resource classes is also simple, by the use of the Pan-Wai. For example, there is a transaction that uses some kind of resource. If this transaction can be represented by a function, then the resource allocation can be represented simply by using a resource variable that is allocated at the entrance of the function. For example:
void transaction1(const char *res_name)
{
Resource res(res_name);
// 后面 是使用资源res
}