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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:17:03+00:00 2026-06-13T07:17:03+00:00

Code : #include<iostream> using namespace std; class A{ public: A(){cout << A() Constructor <<

  • 0

Code :

#include<iostream>
using namespace std;
class A{
    public:
    A(){cout << "A() Constructor " << endl;
        throw 1;
    }

};


int main(){
A* p=0;
cout << p << endl; // p value is 0
try{
p=new A(); // object is not fully constructed, no address is returned to p. for future deallocation

}
catch(...){cout << "Exception" << endl;}
cout << p << endl; // this will output that p has the value 0,proof that no address was returned to p.


}

memory is allocated for A object in the heap, the address of the memory is passed to the constructor
but when the constructor throw 1; then object of type A will not considered to be a fully constructed object. so no
pointer will be returned the pointer p. feel free to correct me if I’ve understood something wrong.

Questions:
1) So my question is how it is possible in such case to deallocate memory for the A object. Im not talking about
The destructor call, but the deallocation of memory.

2) what about when I create a local object of type A inside main function. and obviously it wont be fully constructed either. when does this object get deallocated (ofcourse after calling the destructor of fully construted subobjects ).

  • 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-06-13T07:17:03+00:00Added an answer on June 13, 2026 at 7:17 am

    The allocation done by new A() consists of multiple steps:

    1. Allocate memory using operator new(sizeof(A)).
    2. Try to default construct an object in the allocated memory.
    3. When default construction fails, operator delete(x) is called with the address obtained earlier.

    More generally, if you use an overloaded operator new(), e.g., new(allocator) A() which calls operator new(size_t, Alloctor) (where allocator is convertible to Allocator), a corresponding operator delete() is called.

    As part of exception throwing, all partially constructed subobjects are destroyed (in the reverse order in which they were constructed) as well. That is, if an exception is thrown from the constructor of an object, you don’t need to care about clean-up (assuming, all subobjects correctly look after the resources they have allocated). This is true independent on whether the object is allocated on the heap or the stack.

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

Sidebar

Related Questions

Consider this code: #include <iostream> using namespace std; class hello{ public: void f(){ cout<<f<<endl;
Code: #include<iostream> using namespace std; class B{ public: int b; B(int x):b(x){ cout <<
#include<iostream> using namespace std; class A { public: int i; A() {cout<<A()<<endl;} ~A() {cout<<~A()<<endl;}
Example code: #include <cstdlib> #include <iostream> using namespace std; class A { public: A(int
I have this code #include <iostream> using namespace std; class Test{ public: int a;
#include <iostream> using namespace std; class X { public: X() { cout<<Cons<<endl; } X(const
Look at this code please: #include <iostream> using namespace std; class Ratio { public:
The following code #include <iostream> using namespace std; int main() { const char* const
Consider the following code: #include <iostream> using namespace std; int main() { int x,
Here is some C++ code: #include <iostream> using namespace std; class m { public:

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.