473,383 Members | 1,843 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Constructor/destructor call order

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;

2 4700
weaknessforcats
9,208 Expert Mod 8TB
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
Thanks weaknessforcats! A very clear and helpful answer.
Nov 6 '10 #3

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

Similar topics

1
by: Dave Theese | last post by:
Hello all, Please see the two questions and output embedded in the program below. Thank you! Dave #ifdef WIN32 #pragma warning(disable: 4786)
8
by: ctick | last post by:
When defining a clas and no constructor and destructor provided, compiler generates both. What're the need for this since they do nothing as to constructing/destructing an obejct. What's...
23
by: Fabian Müller | last post by:
Hi all, my question is as follows: If have a class X and a class Y derived from X. Constructor of X is X(param1, param2) . Constructor of Y is Y(param1, ..., param4) .
8
by: Arne Claus | last post by:
Hi I've got a little question about the following construct: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - class a { public: a()...
5
by: Preben | last post by:
Hi, I get this error when trying to compile: -------- # g++ -c KGreyImage.cpp KGreyImage.cpp:25: error: expected constructor, destructor, or type conversion before '*' token --------
5
by: Damien | last post by:
Hi all, I'm using a pretty standard C++ Singleton class, as below: template <typename T> class Singleton { public: static T* Instance() {
2
by: algatt | last post by:
Hello, I am trying to compile the TPIE files but there is a file that's constantly giving errors about the templates. I am using gcc 3.4.5 on Eclipse using Windows XP. The following is the code of...
5
by: Warren Tang | last post by:
Hello, Does C++ allow a constructor to call another constructor of the same class? (I am focusing on the language abilities. I know the C# language allow this.) The following sample can...
10
by: preeya | last post by:
Hi, I have written the following program: ------------------------------------------------------------------------------------------------------------- 1 #include <stdio.h> 2 #include...
2
by: prw8864 | last post by:
I can not see what is causing this error.... iterator has been defined properly, but the error seems to point to the interator type. Errors I get with g++ svector.c++ -o svector 2> ./err_txt...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.