raii

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

安稳与你 提交于 2019-11-29 03:26:16
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 would be great if I could wrap the handle in some kind of wrapper class which would automatically call

CUDA: Wrapping device memory allocation in C++

别说谁变了你拦得住时间么 提交于 2019-11-29 00:58:34
问题 I'm starting to use CUDA at the moment and have to admit that I'm a bit disappointed with the C API. I understand the reasons for choosing C but had the language been based on C++ instead, several aspects would have been a lot simpler, e.g. device memory allocation (via cudaMalloc ). My plan was to do this myself, using overloaded operator new with placement new and RAII (two alternatives). I'm wondering if there are any caveats that I haven't noticed so far. The code seems to work but I'm

Can Tail Call Optimization and RAII Co-Exist?

不问归期 提交于 2019-11-28 22:50:44
I can't think of a true RAII language that also has tail call optimization in the specs, but I know many C++ implementations can do it as an implementation-specific optimization. This poses a question for those implementations that do: given that destructors are invoked at the end of a automatic variable's scope and not by a separate garbage collection routine, doesn't it violate TCO's constraint that a recursive call must be the last instruction at the end of a function? For example:- #include <iostream> class test_object { public: test_object() { std::cout << "Constructing...\n"; } ~test

Do programmers of other languages, besides C++, use, know or understand RAII?

末鹿安然 提交于 2019-11-28 22:12:20
问题 I've noticed RAII has been getting lots of attention on Stackoverflow, but in my circles (mostly C++) RAII is so obvious its like asking what's a class or a destructor. So I'm really curious if that's because I'm surrounded daily, by hard-core C++ programmers, and RAII just isn't that well known in general (including C++), or if all this questioning on Stackoverflow is due to the fact that I'm now in contact with programmers that didn't grow up with C++, and in other languages people just don

RAII tutorial for C++ [closed]

杀马特。学长 韩版系。学妹 提交于 2019-11-28 16:23:02
I'd like to learn how to use RAII in c++. I think I know what it is, but have no idea how to implement it in my programs. A quick google search did not show any nice tutorials. Does any one have any nice links to teach me RAII? utnapistim There's nothing to it (that is, I don't think you need a full tutorial). RAII can be shortly explained as "Every resource requiring cleanup should be given to an object's constructor." In other words: Pointers should be encapsulated in smart pointer classes (see std::auto_ptr, boost::shared_ptr and boost::scoped_ptr for examples). Handles requiring cleanup

Implementing RAII in pure C?

心不动则不痛 提交于 2019-11-28 15:28:50
问题 Is it possible to implement RAII in pure C? I assume it isn't possible in any sane way, but perhaps is it possible using some kind of dirty trick. Overloading the standard free function comes to mind or perhaps overwriting the return address on the stack so that when the function returns, it calls some other function that somehow releases resources? Or maybe with some setjmp/longjmp trick? This is of a purely academic interest and I have no intention of actually writing such unportable and

Local variable scope question

给你一囗甜甜゛ 提交于 2019-11-28 13:29:46
Why is the following code prints "xxY"? Shouldn't local variables live in the scope of whole function? Can I use such behavior or this will be changed in future C++ standard? I thought that according to C++ Standard 3.3.2 " A name declared in a block is local to that block. Its potential scope begins at its point of declaration and ends at the end of its declarative region. " #include <iostream> using namespace std; class MyClass { public: MyClass( int ) { cout << "x" << endl; }; ~MyClass() { cout << "x" << endl; }; }; int main(int argc,char* argv[]) { MyClass (12345); // changing it to the

C# - Are objects immediately destroyed when going out of scope?

夙愿已清 提交于 2019-11-28 10:05:05
Can I trust that an object is destroyed and its destructor is called immediately when it goes out of scope in C#? I figure it should since many common coding practices (e.g. transaction objects) rely on this behaviour, but I'm not very used to working with garbage collection and have little insight to how such languages usually behave. Thanks. Peter Lillevold Nope, .Net and hence C# relies on a garbage collection memory management. So destructors (which in .Net is called finalizers) are not called until GC finds it proper to destroy the objects. Additionally: most "regular" objects in C# don't

How to initialize an object using async-await pattern

允我心安 提交于 2019-11-28 09:05:46
I'm trying to follow RAII pattern in my service classes, meaning that when an object is constructed, it is fully initialized. However, I'm facing difficulties with asynchronous APIs. The structure of class in question looks like following class ServiceProvider : IServiceProvider // Is only used through this interface { public int ImportantValue { get; set; } public event EventHandler ImportantValueUpdated; public ServiceProvider(IDependency1 dep1, IDependency2 dep2) { // IDependency1 provide an input value to calculate ImportantValue // IDependency2 provide an async algorithm to calculate

C++ read the whole file in buffer [duplicate]

為{幸葍}努か 提交于 2019-11-28 06:48:34
This question already has an answer here: What is the best way to read an entire file into a std::string in C++? 10 answers What is a good approach to read the whole file content in a buffer for C++? While in plain C I could use fopen(), fseek(), fread() function combination and read the whole file to a buffer, is it still a good idea to use the same for C++? If yes, then how could I use RAII approach while opening, allocating memory for buffer, reading and reading file content to buffer. Should I create some wrapper class for the buffer, which deallocates memory (allocated for buffer) in it's