raii

Why use one vs the other: `boost::shared_array` VS `boost::shared_ptr<std::vector>`?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-30 07:07:20
问题 So to deal with large blobs of memory either for an image or similar there are clearly lots of options. Since I'm a fan of smart pointers and RAII I'm wondering about whether it's smarter to go with : a shared_ptr to a std::vector or to go with a shared_array pointing to a dynamically allocated array. What are the conceptual, practical, and performance implications of choosing one vs the other? 回答1: It's the same as comparing std::vector vs. C array. Think about shared_array as a RAII C array

Where's the proper (resource handling) Rule of Zero? [closed]

好久不见. 提交于 2019-12-29 07:33:08
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 6 years ago . Here's an article that talks about an idiom named Rule of Zero. Here's an excerpt: class module { public: explicit module(std::wstring

What wrapper class in C++ should I use for automated resource management?

删除回忆录丶 提交于 2019-12-29 05:50:13
问题 I'm a C++ amateur. I'm writing some Win32 API code and there are handles and weirdly compositely allocated objects aplenty. So I was wondering - is there some wrapper class that would make resource management easier? For example, when I want to load some data I open a file with CreateFile() and get a HANDLE . When I'm done with it, I should call CloseHandle() on it. But for any reasonably complex loading function there will be dozens of possible exit points, not to mention exceptions. So it

Are destructors called after a throw in C++?

独自空忆成欢 提交于 2019-12-27 12:17:07
问题 I ran a sample program and indeed destructors for stack-allocated objects are called, but is this guaranteed by the standard? 回答1: Yes, it is guaranteed (provided the exception is caught), down to the order in which the destructors are invoked: C++11 15.2 Constructors and destructors [except.ctor] 1 As control passes from a throw-expression to a handler, destructors are invoked for all automatic objects constructed since the try block was entered. The automatic objects are destroyed in the

RAII and system resource cleanup

℡╲_俬逩灬. 提交于 2019-12-24 01:25:40
问题 RAII is a good solution for resource cleanup. However, RAII is based on stack unwinding. If process terminates abnormally, the stack won't be unwinded. It means RAII won't work in this situation. For process life-time's resource, it's nothing to worry about, but for file system life tiem or kernal life time resource, such as file, message queue, semaphore, shared memory, it will be a problem. How can I cleanup the system(fs and kernal) resource in a reliable way? Example: A shared file will

The meaning of the term - Resource Acquisition Is Initialization

左心房为你撑大大i 提交于 2019-12-23 12:17:13
问题 I know what RAII does. It is all about preventing memory leaks etc. when/if a code throws an exception. Now, I wish to understand the meaning of that smart term. http://en.wikipedia.org/wiki/Acquisition Acquisition means acquiring something. So, when we say that resource acquiring is initialization , what does that mean? I am just talking about the meaning of the term here, not about the concept in general. 回答1: It has been said before (possibly by Scott Meyers, I can't remember), that RAII

Stack allocated RAII objects vs DI principle

Deadly 提交于 2019-12-22 04:39:10
问题 In C++ I often use RAII-style objects to make code more reliable and allocate them on stack to make code more performant (and to avoid bad_alloc). But creating an object of concrete class on stack violates the dependency inversion (DI) principle and prevents mocking this object. Consider the following code: struct IInputStream { virtual vector<BYTE> read(size_t n) = 0; }; class Connection : public IInputStream { public: Connection(string address); virtual vector<BYTE> read(size_t n) override;

RAII-style C++ class for linked list Nodes

不羁的心 提交于 2019-12-21 18:13:26
问题 I'm playing with linked lists as an exercise at the moment. The examples I'm looking at in the Cracking The Coding Interview book have no LinkedList (manager) class, just Nodes, and you hang on to the head Node in your main function. I looked up C++ implementations, but most seem to be more C-style than C++, i.e. not object-oriented. They use structs, no classes, and have a static method for deleting the list, which you need to explicitly remember to call. I wanted to write a sensible RAII

Is it possible to write/wrap the exception handling components (try,catch) in different class?

人盡茶涼 提交于 2019-12-21 12:36:56
问题 This is about wrapping the exception handling logic in some sort of class. While writing c++ code, many time we need to catch many type/variants of exception depending on what client throw. This lead us to write similar types of code(many times) in catch() clause. In below sample example, I have written the function(), which can throw exception in the many possible form. I wanted to know is it possible to write/wrap such logic in the form of class so that end user would have to write similar

RAII in Python: What's the point of __del__?

泄露秘密 提交于 2019-12-21 12:01:45
问题 At first glance, it seems like Python's __del__ special method offers much the same advantages a destructor has in C++. But according to the Python documentation (https://docs.python.org/3.4/reference/datamodel.html), there is no guarantee that your object's __del__ method ever gets called at all! It is not guaranteed that __del__ () methods are called for objects that still exist when the interpreter exits. So in other words, the method is useless! Isn't it? A hook function that may or may