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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:05:10+00:00 2026-05-23T11:05:10+00:00

Suppose I have a pointer to a dynamically allocated array of 10 elements: T*

  • 0

Suppose I have a pointer to a dynamically allocated array of 10 elements:

T* p = new T[10];

Later, I want to release that array:

delete[] p;

What happens if one of the T destructors throws an exception? Do the other elements still get destructed? Will the memory be released? Will the exception be propagated, or will program execution be terminated?

Similarly, what happens when a std::vector<T> is destroyed and one of the T destructors throws?

  • 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-23T11:05:11+00:00Added an answer on May 23, 2026 at 11:05 am

    I can not see it explicitly called out in the standard:

    Just that they will be called in reverse order of creation

    5.3.5 Delete [expr.delete]

    6 If the value of the operand of the delete-expression is not a null pointer value, the delete-expression will invoke the destructor (if any) for the object or the elements of the array being deleted. In the case of an array, the elements will be destroyed in order of decreasing address (that is, in reverse order of the completion of their constructor; see 12.6.2).

    And that the memory deallocation will be done even if the exception is thrown:

    7 If the value of the operand of the delete-expression is not a null pointer value, the delete-expression will call a deallocation function (3.7.4.2). Otherwise, it is unspecified whether the deallocation function will be called. [ Note: The deallocation function is called regardless of whether the destructor for the object or some element of the array throws an exception. — end note ]

    I tried the following code in G++ and it shows that that no more destructors get called after the exception:

    #include <iostream>
    int id = 0;
    class X
    {
        public:
             X() {   me = id++; std::cout << "C: Start" << me << "\n";}
            ~X() {   std::cout << "C: Done " << me << "\n";
                     if (me == 5) {throw int(1);}
                 }
        private:
            int me;
    };
    
    int main()
    {
        try
        {
            X       data[10];
        }
        catch(...)
        {
            std::cout << "Finished\n";
        }
    }
    

    Execute:

    > g++ de.cpp
    > ./a.out
    C: Start0
    C: Start1
    C: Start2
    C: Start3
    C: Start4
    C: Start5
    C: Start6
    C: Start7
    C: Start8
    C: Start9
    C: Done 9
    C: Done 8
    C: Done 7
    C: Done 6
    C: Done 5
    Finished
    

    Which all leads back to this (very old answer):
    throwing exceptions out of a destructor

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

Sidebar

Related Questions

Suppose I have some pointer, which I want to reinterpret as static dimension array
Suppose I have a stringbuilder in C# that does this: StringBuilder sb = new
So, I have a function which returns a pointer to a dynamically allocated double,
Please suppose I have a function that accepts a pointer as a parameter. This
Suppose that we have a pointer-to-class member pointing at a field of a class.
suppose we have an unsafe context, because I want to have a pointer to
Suppose you have a class with a private pointer to an array. How can
Suppose I have a STL map where the values are pointers, and I want
suppose I have a class with many explicit (statically allocated) members and few pointers
Suppose I have: Toby Tiny Tory Tily Is there an algorithm that can easily

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.