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 861361
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T08:58:03+00:00 2026-05-15T08:58:03+00:00

I have a simple question hopefully – how does one free memory which was

  • 0

I have a simple question hopefully – how does one free memory which was allocated in the try block when the exception occurs? Consider the following code:

try
 {
  char *heap = new char [50];
        //let exception occur here
  delete[] heap;
 }
 catch (...)
 {
  cout << "Error, leaving function now";
  //delete[] heap; doesn't work of course, heap is unknown to compiler
  return 1;
 }

How can I free memory after the heap was allocated and exception occurred before calling delete[] heap? Is there a rule not to allocate memory on heap in these try .. catch blocks?

  • 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. Editorial Team
    Editorial Team
    2026-05-15T08:58:04+00:00Added an answer on May 15, 2026 at 8:58 am

    Study the RAII idiom (Resource Acquisition Is Initialization)! See e.g. the Wikipedia article on RAII.

    RAII is just the general idea. It is employed e.g. in the C++ standard library’s std::unique_ptr or std::shared_ptr template classes.


    Very brief explanation of the RAII idiom:

    Basically, it is the C++ version of try..finally blocks found in some other languages. The RAII idiom is arguably more flexible.

    It works like this:

    • You write a wrapper class around your resource (e.g. memory). The destructor is responsible for freeing the resource.

    • You create, as a local (automatic) variable, an instance of your wrapper class in a scope. Once program execution leaves that scope, the object’s destructor will be called, thereby releasing the resource (e.g. memory).

    The important point is that it doesn’t matter how the scope is exited. Even if an exception is thrown, the scope is still exited and the wrapper object’s destructor is still called.


    Very crude example:

    // BEWARE: this is NOT a good implementation at all, but is supposed to
    // give you a general idea of how RAII is supposed to work:
    template <typename T>
    class wrapper_around
    {
      public:
        wrapper_around(T value)
            : _value(value)
        { }
        T operator *()
        {
            return _value;
        }
        virtual ~wrapper_around()
        {
            delete _value;  // <-- NOTE: this is incorrect in this particular case;
                            // if T is an array type, delete[] ought to be used
        }
      private:
        T _value;
    };
    // ...
    
    {
        wrapper_around<char*> heap( new char[50] );
        // ... do something ...
    
        // no matter how the { } scope in which heap is defined is exited,
        // if heap has a destructor, it will get called when the scope is left.
        // Therefore, delegate the responsibility of managing your allocated
        // memory to the `wrapper_around` template class.
        // there are already existing implementations that are much better
        // than the above, e.g. `std::unique_ptr` and `std::shared_ptr`!
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hopefully a simple question although one I have found impossible to answer myself using
I have a (hopefully) a relatively simple question. How do I tell Android which
Hopefully a simple question. Is it possible to have more than one report inside
I hopefully have a simple question I just cannot get anything I try to
I have a simple question, which should hopefully have a quick answer. The code
I have a (hopefully) simple question. Is it possible to use SSHD tunneling to
I'm just getting started with Require.JS and I have a (hopefully) simple question. I'm
This is hopefully a simple question: I have an OpenGL texture and would like
hopefully a pretty simple question this time. I have a Select method in a
Hopefully this is a simple question and I have just overlooked something somewhere... I

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.