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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:26:49+00:00 2026-05-28T05:26:49+00:00

I am trying to catch the bad_alloc exception in order to prove that the

  • 0

I am trying to catch the bad_alloc exception in order to prove that the destructors are used.

Here is my Object:

#include "Obj.h"
#include<iostream>
using namespace std;

Obj::Obj() {
d = new double[200000000];
}
Obj::~Obj() {
cout << "destroyed \n";
}

And the main method:

#include "Obj.h"
#include <iostream>
using namespace std;
int main(){
Obj* ptr[1000000];
try{
    for(int i=0; i<1000; i++){
        ptr[i] = new Obj();
    }
} catch(bad_alloc){
    cout<<"EXCEPTION";
}
}

Instead of catching the exception, my program stops and tries to look for a solution online(Windows).
Why is this happening?

EDIT
I am getting now the exception, but I should prove that the destructor is used BEFORE the exception is thrown. How should I do that?

  • 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-28T05:26:50+00:00Added an answer on May 28, 2026 at 5:26 am

    The problem occurs before you even start dynamically allocating objects. If you run the program with the debugger attached, you will see that the program is terminated due to a stack overflow. Why?

    Obj* ptr[1000000];
    

    You cannot declare so large an object with automatic storage duration. When main is entered, it attempts to allocate space on the stack for this object and fails to do so, causing a stack overflow structured exception to be thrown. Your application does not handle this exception, so the runtime terminates the program.

    Note, however, that the Obj destructor will never be called by your program. When you dynamically allocate an object using new, you are responsible for destroying it using delete. Since you do not ever call delete to destroy the objects you created, they are never destroyed.

    If you were to use, say, a std::vector<std::unique_ptr<Obj>> instead (or, for that matter, just a std::vector<Obj>), you would see that the destructor would be called for every fully-created Obj object.

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

Sidebar

Related Questions

I have read several times that using catch (Exception ex) { Logger.LogError(ex); } without
I'm trying to catch a ClassCastException when deserializing an object from xml. So, try
I am trying to debug a property not found exception that is only happening
I'm trying to catch a double-click event in a TreeView's empty area to create
I'm trying to catch an event when a user clicks a blank space on
Hi I am am trying to catch mouse movements for a MouseOver function in
In C++, I'm trying to catch all types of exceptions in one catch (like
Im working on a response filter. Im trying to catch all expresion: $sometext.sometext$ with
I'm having a bit of a play around with IIS7, just trying to catch
I am trying to set and catch the URL variable It begins with a

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.