By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,948 Members | 663 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,948 IT Pros & Developers. It's quick & easy.

Constructor/destructor call order

P: 2
Hi,

The following piece of code
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. class A
  4. {
  5. public:
  6.     A()
  7.     {
  8.         printf("A constructed\n");
  9.     }
  10.  
  11.     ~A()
  12.     {
  13.         printf("A destroyed\n");
  14.     }
  15.  
  16.     void dummyMethod()
  17.     {
  18.         objectB.methodB(&objectC);
  19.  
  20.     }
  21.  
  22. private:
  23.     B objectB;
  24.     C objectC;
  25. };
  26.  
  27.  
  28. int main(int argc, char *argv[])
  29. {
  30.     A *ptA = new A;
  31.  
  32.     use(A);
  33.  
  34.     delete A;
  35.  
  36.     return 0;
  37. }
  38.  
outputs this:
Expand|Select|Wrap|Line Numbers
  1. B constructed
  2. A constructed
  3. ...
  4. A destroyed
  5. B destroyed
  6. *** glibc detected *** ./capture: free(): invalid next size (fast): 0x0000000007fb3090 ***
  7. ...
  8.  
If I comment out the line where methodB is used, I get no B construction/destruction output.
What I desperately need to know is:
1) How can B be constructed before A?
2) Is this the reason behind the glibc error?

Thanks.
Nov 4 '10 #1

✓ answered by weaknessforcats

This is not really about constructor/destructor call order.

Your question really is why are member variable constructors called before the class constructor.

The sequence is:

1) the compiler allocates memory for A.
2) the compiler calls the constructor for each member variable. This is your call to B::B.
3) once the A object is allocated an all member variables are initialized, then the compiler calls A::A.

When A is destroyed:

1) the compiler calls A::~A so that you can clean up any member variables you need to.
2) the compilers calls the destructor on each member variable. This is your call to B::~B.

Remember, member variable constructors are called in the order the member variables are declared in the class. The membr variable destructors are called in the reverse order of declaration in the class.

Finally, you delete the object using the pointer to the object and not the class name:

delete ptA;

Share this Question
Share on Google+
2 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
This is not really about constructor/destructor call order.

Your question really is why are member variable constructors called before the class constructor.

The sequence is:

1) the compiler allocates memory for A.
2) the compiler calls the constructor for each member variable. This is your call to B::B.
3) once the A object is allocated an all member variables are initialized, then the compiler calls A::A.

When A is destroyed:

1) the compiler calls A::~A so that you can clean up any member variables you need to.
2) the compilers calls the destructor on each member variable. This is your call to B::~B.

Remember, member variable constructors are called in the order the member variables are declared in the class. The membr variable destructors are called in the reverse order of declaration in the class.

Finally, you delete the object using the pointer to the object and not the class name:

delete ptA;
Nov 4 '10 #2

P: 2
Thanks weaknessforcats! A very clear and helpful answer.
Nov 6 '10 #3

Post your reply

Sign in to post your reply or Sign up for a free account.