Managing file descriptors and sockets with RAII (Resource Acquisition Is Initialization) in C++ ensures that resources are properly released when they go out of scope, preventing memory leaks and resource exhaustion. The principle behind RAII is to associate resource management with object lifetime. By wrapping file descriptors and sockets in RAII-style classes, you can ensure automatic cleanup.
In this example, we will create a simple RAII class that manages a file descriptor:
class FileDescriptor {
int fd;
public:
FileDescriptor(const char* path, int flags) {
fd = open(path, flags);
if (fd == -1) {
throw std::runtime_error("Failed to open file");
}
}
~FileDescriptor() {
if (fd != -1) {
close(fd);
}
}
int get() const { return fd; }
// Delete copy constructor and assignment operator
FileDescriptor(const FileDescriptor&) = delete;
FileDescriptor& operator=(const FileDescriptor&) = delete;
};
// Usage
int main() {
try {
FileDescriptor fd("example.txt", O_RDONLY);
// Use fd.get() to read from the file
} catch (const std::exception& e) {
std::cerr << e.what() << '\n';
}
}
How do I avoid rehashing overhead with std::set in multithreaded code?
How do I find elements with custom comparators with std::set for embedded targets?
How do I erase elements while iterating with std::set for embedded targets?
How do I provide stable iteration order with std::unordered_map for large datasets?
How do I reserve capacity ahead of time with std::unordered_map for large datasets?
How do I erase elements while iterating with std::unordered_map in multithreaded code?
How do I provide stable iteration order with std::map for embedded targets?
How do I provide stable iteration order with std::map in multithreaded code?
How do I avoid rehashing overhead with std::map in performance-sensitive code?
How do I merge two containers efficiently with std::map for embedded targets?