Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 57517
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T17:42:13+00:00 2026-05-10T17:42:13+00:00

I’ve noticed RAII has been getting lots of attention on Stackoverflow, but in my

  • 0

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’t use/know about RAII?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T17:42:13+00:00Added an answer on May 10, 2026 at 5:42 pm

    For people who are commenting in this thread about RAII (resource acquisition is initialisation), here’s a motivational example.

    class StdioFile {     FILE* file_;     std::string mode_;      static FILE* fcheck(FILE* stream) {         if (!stream)             throw std::runtime_error('Cannot open file');         return stream;     }      FILE* fdup() const {         int dupfd(dup(fileno(file_)));         if (dupfd == -1)             throw std::runtime_error('Cannot dup file descriptor');         return fdopen(dupfd, mode_.c_str());     }  public:     StdioFile(char const* name, char const* mode)         : file_(fcheck(fopen(name, mode))), mode_(mode)     {     }      StdioFile(StdioFile const& rhs)         : file_(fcheck(rhs.fdup())), mode_(rhs.mode_)     {     }      ~StdioFile()     {         fclose(file_);     }      StdioFile& operator=(StdioFile const& rhs) {         FILE* dupstr = fcheck(rhs.fdup());         if (fclose(file_) == EOF) {             fclose(dupstr); // XXX ignore failed close             throw std::runtime_error('Cannot close stream');         }         file_ = dupstr;         return *this;     }      int     read(std::vector<char>& buffer)     {         int result(fread(&buffer[0], 1, buffer.size(), file_));         if (ferror(file_))             throw std::runtime_error(strerror(errno));         return result;     }      int     write(std::vector<char> const& buffer)     {         int result(fwrite(&buffer[0], 1, buffer.size(), file_));         if (ferror(file_))             throw std::runtime_error(strerror(errno));         return result;     } };  int main(int argc, char** argv) {     StdioFile file(argv[1], 'r');     std::vector<char> buffer(1024);     while (int hasRead = file.read(buffer)) {         // process hasRead bytes, then shift them off the buffer     } } 

    Here, when a StdioFile instance is created, the resource (a file stream, in this case) is acquired; when it’s destroyed, the resource is released. There is no try or finally block required; if the reading causes an exception, fclose is called automatically, because it’s in the destructor.

    The destructor is guaranteed to be called when the function leaves main, whether normally or by exception. In this case, the file stream is cleaned up. The world is safe once again. 😀

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to count how many characters a certain string has in PHP, but
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
Basically, what I'm trying to create is a page of div tags, each has
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.