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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:17:01+00:00 2026-05-24T06:17:01+00:00

Exceptional C++ mentions the following code template <class T> class Stack { public: Stack();

  • 0

Exceptional C++ mentions the following code

template <class T> class Stack 
{
    public:
      Stack();
      ~Stack();

      /*...*/

    private:
       T*     v_;      // ptr to a memory area big
       size_t vsize_;  //  enough for 'vsize_' T's
       size_t vused_;  // # of T's actually in use
};



template<class T> 
Stack<T>::Stack()
        : v_(new T[10]),  // default allocation
          vsize_(10),
          vused_(0)       // nothing used yet
{ 
}

It says that If one of the T constructors threw, then any T objects that were fully constructed were properly destroyed and, finally, operator delete was automatically called to release the memory. That makes us leakproof.

My understanding was that if a constructor throws an exception, the application should cleanup any allocated resources. How is the above leakproof?

  • 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-24T06:17:01+00:00Added an answer on May 24, 2026 at 6:17 am

    Quoting the C++03 standard, §5.3.4/8:

    A new-expression obtains storage for the object by calling an allocation function. If the new-expression terminates by throwing an exception, it may release storage by calling a deallocation function. If the allocated type is a non-array type, the allocation function’s name is operator new and the deallocation function’s name is operator delete. If the allocated type is an array type, the allocation function’s name is operator new[] and the deallocation function’s name is operator delete[].

    §5.3.4/17:

    If any part of the object initialization described above terminates by throwing an exception and a suitable deallocation function can be found, the deallocation function is called to free the memory in which the object was being constructed, after which the exception continues to propagate in the context of the new-expression.

    Consequently, if any T constructor throws an exception, the runtime will destroy any already-created subobjects of the T instance whose constructor threw, then call operator delete[] on the array as a whole, destroying any already-created elements and deallocating the array’s memory.

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

Sidebar

Related Questions

I am creating the bitmap from a linearlayout with the following code public static
Which of the following exception notification solutions is the best? Exceptional Hoptoad exception_notification exception_logger
Take the following code as a sample: procedure TForm1.Button1Click(Sender: TObject); var Obj: TSomeObject; begin
If I run the following code from a terminal, I get a helpful error
I'm trying to get the first executing assembly using the following code: AssemblyName entryAssembly
Adding Exceptional Handling is a good practise, but I have a doubt, In our
I understand that Exceptions are for exceptional cases [a], but besides just being repeated
A lot of developers say only throw exceptions in truly exceptional circumstances. One of
I'm looking for websites that showcase screenshots of applications with exceptional UI design. I'm
It's my understanding that common wisdom says to only use exceptions for truly exceptional

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.