There is no finally in C + +, then you should close the resource

Source: Internet
Author: User
Tags finally block

This is an interesting post

Original link: http://bbs.csdn.net/topics/90070457

Landlord:

There is no finally in C + +, where should you close the resource?

C + + Try{}catch () {}, why is there no finally{} block to free up resources?
such as a method in a class, a local variable
File * FILE=NULL;
try{
File=fopen ("Aaa.txt", "R");
And then perform some operations.
}
Last fclose (file);
If the file operation has an exception, it needs to be closed. For Java, it is closed in the finally{} block. Whether or not an exception occurs, it must be closed!
and in C + +, how do you ensure that you close this file?

Daniel A:

RAII is commonly used in C + +, resource aquisition is initialization.
is to encapsulate the resource into a class, encapsulate the initialization of the resource in the constructor, and release the wrapper in the destructor. To instantiate a local object when the resource is partially used.
When an exception is thrown, the destructor is automatically called because the local object is out of scope, guaranteeing that the resource is freed

A simple example:
try {
File f ("xxx.ttt");
Other file operation
}//file Pointer is released here
catch {
Exception process
}

Daniel B:

Polarislee (North Star) (where is Beijing so big, where is my house) said "when throwing an exception, because the local object is out of scope, automatically call the destructor, will ensure that the resources are released" has a name, called "Stack Expansion", is in the "Bomb stack" When the structure of the object in the stack is automatically refactored, so that there is no "resource leak".

Daniel C:

Said C + + has finally all identified as java/c# spies, said C + + has __finally all identified as MS spy ... ^^

Standard C + + does not have a statement structure similar to finally. C #/Java guarantees that the code of the finally block will be executed regardless of whether an exception occurs, whereas in C + +, the destructor of the local variable is guaranteed to execute regardless of whether an exception occurs, so the solution to the finally block,c++ is POLARISL The EE Warrior said RAII.

Daniel A:

Raii is the principle of automatic destruction of stack variables when they leave the scope. Most of the people upstairs mentioned the smart pointers are only concerned about the memory leaks, but the landlord is said to be resource management, such as file handles and sockets and other things, only the pointer here is powerless.

Another underlined __try{}__catch{}__finally{} is the standard exception handling for Windows systems, which is not the C + + standard syntax but is used to capture and handle exceptions generated by some Windows systems, such as: memory access out of bounds, except for 0 exceptions.

As for the thought that packaging resources write a class too troublesome people may wish to think:
If it is a large system, a class of resources may be used in thousands of places, then the benefits of this package is far greater than the trouble of writing a category, is not it:)

Daniel D:

With Raii, it's about wrapping up resources, the benefits of using destructors to be automatically called by the compiler
The wrapper class also holds a handle to the resource, just as you would with an int variable
Can't you int also want new, in fact this should be <<imperfect C++>> said Rrid,
Resource release is a destructor, and wrapper classes accept external resource handles and release in destructors, which is the beauty of C + +.
If there is a finally, always remember to close the resources is a very annoying thing,
So although there is a finally in C #, it also provides
using (A A = new A ())
{
}
This type of, beyond its scope, calls its Dispose function
It makes the program look elegant.

Daniel E:

People who use C + + should have a common understanding of the need to use a delegate for resources who apply for who is released. When a resource is used only in a function, it must be released in that function.

C + + does not need to finally, about the problem in Java, in C + + is not exist, on the contrary, with that kind of thing will bring problems, because the finally setting itself is a useless garbage. As I said before, Java has that kind of thing is a way, is purely helpless, in C + + it when the baby like conspicuous, swarm the follow-up, do not let people laugh!
And, more importantly, the finally mechanism used by Java is not secure, and if there is a quantum computer now, it is a loophole first. At present, it is not exposed to the harmfulness, but also with the key areas of application of less.

Daniel F:

__try, __except, and __finally are the SEH mechanisms of windows, regardless of language.

The try and catch are exception handling mechanisms for C + +.

C does not provide a language-level exception handling mechanism.

Daniel G joked that:

Describes one of the simplest ways to close a resource, very effective--hold power for more than 4 seconds, and all resources are automatically released.

Simply define a final empty macro.
And then it can be written
#ifndef final
#define Final
#endif

file* file;
Try
{....
}
catch (...)
{....
}
Final
{....
}
There's no grammatical problem.

Oh

Daniel H:

Finally is not part of standard C + +, and now the _finally that we are trying to build are the extensions of each compiler vendor. <the C + + programming language> author Bjarne Stroustrup thinks finally in C + + is redundant, and its work should be done by a destructor.

The stubborn landlord finally said:

I decided to use the Boost smart pointer scheme.
Loki's Scopeguard is too advanced, No.
HP's GC scheme, too ugly. Intrusive too strong.

There is no finally in C + +, then you should close the resource

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.