2

For which one the space is allocated first when the derived class object is created?

whether base class constructor or derived class constructor?

user1615591
  • 21
  • 1
  • 2
  • 1
    [Base first, see here for more detail](http://stackoverflow.com/questions/2517050/c-construction-and-initialization-order-guarantees) – tmpearce Aug 22 '12 at 00:21
  • @close-voter: no, allocation is not the same as initialization. don't vote on things based on your feeling that you don't know. vote only on things you do know. – Cheers and hth. - Alf Aug 22 '12 at 00:33
  • @Cheersandhth.-Alf (First, no I'm not the close-voter) I'm guessing based on the wording about the *constructor* that the terminology of allocation and initialization were likely mixed up in the question, hence the link... perhaps not though. Regardless, I found your answer below to be informative, +1 – tmpearce Aug 22 '12 at 01:16

3 Answers3

5

First,

  • allocation, the reservation of memory which you’re asking about, is different from and precedes initialization (execution of a constructor that essentially sets suitable values in that memory), and

  • the formal (our Holy Standard) and the in-practice differ on whether memory for a most derived object needs to be contiguous, with the formal defining a “region of memory” as possibly non-contiguous, mostly in order to support multiple virtual inheritance.

That said, in practice a most derived object is a single, contiguous chunk of memory that includes space for all base class sub-objects and data member sub-objects, and this chunk is necessarily allocated all at once.

Initialization (calls of constructors) proceeds after the allocation. A new expression guarantees a deallocation if initialization fails by throwing an exception. However, this guarantee is voided if the allocation function that’s employed has extra custom arguments (a so called “placement new”) and no corresponding deallocation function is available, as was the case e.g. for debug builds in early versions of Microsoft’ MFC class framework (it was pretty ironic: a program where initialization failed would leak memory only in debug builds…).

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
1

The space for the derived object holds all of the derived members and all of the base members. There is only one allocation for the derived object, and the allocated memory holds all the pieces of the object.

Pete Becker
  • 74,985
  • 8
  • 76
  • 165
-1

As mentioned in the comment it is the Base class. Logically, since you can access the base public and protected members in the Derived class (including constructor) it will need to be allocated first. Try starting with the following code and play around.

#include <iostream>

class Base
{
    public:
    Base() {std::cout<<"Base CTOR" << std::endl;}
};

class Derived : public Base
{
    public:
    Derived():Base() {std::cout<<"Derived CTOR"<<std::endl;}
};

int main(int argc, char* argv[])
{
    Derived d;
}
MartyE
  • 636
  • 3
  • 7
  • This code looks at the order of construction, not at memory allocation. – Pete Becker Aug 22 '12 at 00:37
  • -1 In practice all memory for an object is allocated as a single chunk, at once. – Cheers and hth. - Alf Aug 22 '12 at 00:45
  • @Alf Are we talking about how the language was defined or how the compiler was implemented? Conceptually, if a memory needs to be initialized first, then it cannot be allocated later, no? The fact that most/all compilers choose to allocate a single chunk for all data is an artifact. But of course, if you show that the spec dictates it, then I'm all ears. Before that, I'd say yes the base needs to be allocated NO LATER than the derived class. – Edy Apr 23 '18 at 19:57