raii Questions
3
Solved
I ran a sample program and indeed destructors for stack-allocated objects are called, but is this guaranteed by the standard?
2
Since when an exception is thrown the only code is ensured to being run is the destructor, code like this might generate a memory leak
std::mutex foo;
foo.lock();
// My code which might throw an e...
10
Solved
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 ...
1
Solved
fn main() {
let k = "fire";
drop(k);
println!("{:?}", k);
}
Playground
Why am I still able to use k after dropping it? Does drop not deref a reference automatically? If yes, then why? What ...
16
4
Solved
Often I'm in a situation where I need a simple RAII wrapper, but I wouldn't want to create a whole new class for this for many reasons including time constraints and organization problems. My quick...
3
Solved
I have been around the C++ community for a while to hear that raw pointers "are evil" and that they should be avoided as much as possible. While one of the main reasons to use smart pointers over r...
Withstand asked 11/7, 2016 at 1:35
2
Solved
It appears that arguments of a function executed via std::async share the lifetime of the future:
#include <iostream>
#include <future>
#include <thread>
struct S
{
S() {
std:...
4
Solved
Do I need to manually call close() when I use a std::ifstream?
For example, in the code:
std::string readContentsOfFile(std::string fileName) {
std::ifstream file(fileName.c_str());
if (file....
6
Solved
in my code I use HANDLEs from windows.h. They are used like
HANDLE h;
if (!openHandleToSomething(arg1, arg2, &h)) {
throw std::exception("openHandleToSomething error");
}
/* Use the handle in...
4
Solved
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 ...
Smelt asked 18/11, 2008 at 18:59
2
Solved
In a typical example of RAII for File I/O on Wikipedia, any errors that occur when closing the file are swallowed:
#include <iostream>
#include <string>
#include <fstream>
#incl...
Apostrophize asked 20/2, 2018 at 8:23
2
Solved
C++ steam objects have state. If one write a piece of code like
using namespace std;
cout << hex << setw(8) << setfill('0') << x << endl;
forgetting setting...
4
Solved
Background: When reading Dr. Stroustrup's papers and FAQs, I notice some strong "opinions" and great advices from legendary CS scientist and programmer. One of them is about shared_ptr in C++0x. He...
Holcman asked 25/12, 2009 at 3:11
4
Solved
I recently found about RAII in c++ and most examples of RAII talk about exception safety. How you can always release resources even if an exception were to be thrown.
The question I have is, if R...
6
Solved
Bjarne Stroustrup writes in his C++ Style and Technique FAQ, emphasis mine:
Because C++ supports an alternative that is almost always better: The "resource acquisition is initialization" techniq...
3
Solved
Say you have a class foo that wraps a collection of some kind of callable objects. foo has a member function run() that iterates over the collection and calls each function object. foo also has a m...
5
Solved
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 ...
Homonym asked 9/4, 2013 at 16:24
3
When I rely on lifetime extension to assign to a class with a non trivial destructor the compiler (both gcc and clang) issue unused variable warnings. Is there anyway to get around this? https://wa...
Benzoate asked 13/11, 2017 at 6:2
6
Solved
Related topic
std::unique_ptr, deleters and the Win32 API
To use a Win32 Handle as a RAII, I can use the following line
std::unique_ptr<std::remove_pointer<HANDLE>::type, decltype(&...
1
Solved
In C++, we can manage resources by objects, i.e. acquiring resource in Ctor, and releasing it in Dtor (RAII). This relies on C++'s automatic destructor invocation. But how is this done under the ho...
2
Solved
C++ so far (unfortunately) doesn't support finally clause for a try statement. This leads to speculations on how to release resources. After studying the question on the internet, although I ...
Witchery asked 13/6, 2017 at 11:54
3
The question is: Could you please help me understand better the RAII macro in C language(not c++) using only the resources i supply at the bottom of this question? I am trying to analyse it in my m...
3
Solved
I'm wondering, for no other purpose than pure curiosity (because no one SHOULD EVER write code like this!) about how the behavior of RAII meshes with the use of goto (lovely idea isn't it).
class ...
1
Solved
I'm implementing an object that owns several resources created from C libraries through FFI. In order to clean up what's already been done if the constructor panics, I'm wrapping each resource in i...
© 2022 - 2024 — McMap. All rights reserved.