#1
  1. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,595
    Rep Power
    4207

    New Puzzle of the [week|month|year]


    Right guys, it's been a while since I posted my Puzzle of the Week post (POTW). Apologies about the inordinate delay, I've been rather swamped at work.

    This week's puzzle is going to be in C++, not C. Assume you have a resource that has to be declared as a pointer. For instance, let's say the resource is a FILE * variable. You open this resource at the top of your function.

    Your problem is to ensure that the resource is closed reliably when the function exits. Note that standard C++ doesn't support a finally keyword or __finally or any other such variation, so don't bother using that one.

    Post your solutions here. As usual, correct solutions will have their handles displayed on my sig-line for the next week or so.

    Cheers and happy hackin'.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    San Francisco Bay
    Posts
    1,939
    Rep Power
    1313
    Yay. :) Here's the first thing that comes to mind:

    c++ Code:
    void myfunction(void) {
     
        struct resource_wrapper {
            resource *res;
     
            resource_wrapper(...) {
                // open the resource
            }
            ~resource_wrapper() {
                // release the resource
            }
        } w(...);
     
        // do stuff with w
    }
    The idea is to let the destructor of the wrapper class handle all the cleanup. Even if the function throws an exception, the local variable w will execute its destructor.
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2005
    Location
    planet<earth_like_t>
    Posts
    365
    Rep Power
    148
    Originally Posted by Scorpions4ever
    This week's puzzle is going to be in C++, not C. Assume you have a resource that has to be declared as a pointer. For instance, let's say the resource is a FILE * variable.
    Lol. Bad example. :p Since you did specify C++, I wouldn't use a FILE* but an fstream which, you know, cleans up after itself...

    Anyhoo, the real answer is: use boost::shared_ptr<> on objects with defined destructors. Because if, for the purpose of resource self-clean-up, I see anything else than that in production code, you'll get a visit from this code nazi.

    That's my extreme, everything-is-black-and-white, no-exceptions answer and I stick to it.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jul 2006
    Posts
    2,270
    Rep Power
    1736
    RAII if your design supports it.
    When you ask a question, be prepared to tell us: what have you tried? If you think you don't need to try anything, we will never be interested in helping you. If you agree with the link, and you refuse to answer that question, you are being a hypocrite.

    Need help with broken code? Your question should be like a good bug report: (1) It has the smallest number of steps to reproduce the problem you see (2) It tells us precisely what you expected to see and (3) It tells us what you saw and how it differed from what you expected. We need all three to help you.
    Want better answers? Tell us what you Googled for and what steps you took to answer your own question.
  8. #5
  9. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,595
    Rep Power
    4207
    Boy, you guys are good. Wrapping it around a class (or a struct), auto_ptr and RAII is what I was looking for. Stroustrup mentions this as the answer as to why C++ doesn't have finally. The Boost library shared_ptr class looks like it will be part of the next C++ standard (at least, looking at TR1's specs on Smart Pointers)
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo

IMN logo majestic logo threadwatch logo seochat tools logo