In the constructor, an exception occurs in the member functions of the class. Can the Destructor be called normally?

Source: Internet
Author: User

Here, I quoted a reply from a post. I think the answer is good:

** In C ++, the memory is divided into heap (heap area)-place the memory dynamically allocated by new, stack (stack area)-place temporary (local) variables, and static storage zone-place static and global variables;

** Non-local static variables are allocated by the compiler before the main () function and initialized (constructed () after completion, the compiler releases the memory occupied by the program, so this part of the memory will not be released as long as the program does not end;

** Local variables are automatically generated and destroyed by the compiler during program execution;
** Heap objects are allocated with memory by new, and must be released by the programmer through Delete;
Memory leaks generally occur on heap objects;
Example:
Class B: public
{
Public:
B (int I): A (I)
{
}
}
The constructor does not dynamically allocate memory, so no matter whether the object construction is successful or not, you don't have to worry about memory leakage. The compiler has done this for you :)
** If there is a dynamic memory allocation in the constructor, you should also use Delete in the destructor to release the memory through new; however, if an exception occurs in the constructor (after the new statement), the constructor will exit if there is no exception handler. Because the object is not fully constructed, the object's destructor will not be called, that is, the delete statement is not executed, causing memory leakage;

** Solution: After using smart pointer (smart pointer), you have already written a lot and won't be long enough. If you still don't know how to contact me.

Summary:

1. Do not throw an exception in constructor and destructor.

2. If an exception occurs in the constructor because the constructor is not fully initialized, The Destructor will not be called when an exception occurs.

3. If an exception occurs to a member function in the class, the Destructor is called. The Code is as follows:

# Include <iostream>
# Include <string>
Using namespace STD;
Class {
Public:
A (int * ){
P = new int;
* P = *;
}
~ A (){
Cout <"Call destructor" <Endl;
Delete P;
}
Void show (){
Int AC = 3;
Try {
Throw AC;
} Catch (INT ){
Cout <"throw an exception" <Endl;
}
}
PRIVATE:
Int * P;

};
Int main (){
Int x = 100;
Int * B = & X;
A A (B );
A. Show ();
Return 0;
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.