Implicit default constructor is not called

Discussion in 'C++' started by Alex Vinokur, Aug 9, 2006.

  1. Alex Vinokur

    Alex Vinokur Guest

    Compiler Green Hills C++, Version 4.0.6

    --- foo.cpp ---
    struct A
    {
    };

    struct B
    {
    B() {}
    };

    struct C
    {
    virtual foo() {}
    };

    int main ()
    {
    A a;
    B b;
    C c;
    return 0;
    }
    ---------------



    --- Mixed: source & asm (Fragments) ---
    int main ()
    0x10314 main: 7c0802a6 mflr r0
    0x10318 main+0x4: 90010004 stw r0, 4(sp)
    0x1031c main+0x8: 9421fff0 stwu sp, -16(sp)
    {
    0x10320 main+0xc: 48000d9d bl _main (0x110bc)
    A a;
    B b;
    0x10324 main+0x10: 38610008 addi r3, sp, 8
    0x10328 main+0x14: 4bfffe15 bl B::B() (0x1013c)
    C c;
    0x1032c main+0x18: 3861000c addi r3, sp, 0xc
    0x10330 main+0x1c: 4bfffeed bl C::C() (0x1021c)
    return 0;
    0x10334 main+0x20: 39800000 li r12, 0
    }
    0x10338 main+0x24: 7d836378 mr r3, r12
    0x1033c main+0x28: 80010014 lwz r0, 0x14(sp)
    0x10340 main+0x2c: 7c0803a6 mtlr r0
    0x10344 main+0x30: 38210010 addi sp, sp, 0x10
    0x10348 main+0x34: 4e800020 blr
    ---------------------------------------


    We can see that:
    * explicit default constructor B::B() is called while building the b
    object;
    * implicit default constructor C::C() is called while building the c
    object;
    * implicit default constructor A::A() is NOT called while building the
    a object.

    So, A::A() is not called. Why?

    Alex Vinokur
    email: alex DOT vinokur AT gmail DOT com
    http://mathforum.org/library/view/10978.html
    http://sourceforge.net/users/alexvn
    Alex Vinokur, Aug 9, 2006
    #1
    1. Advertising

  2. Alex Vinokur

    peter koch Guest

    Alex Vinokur wrote:
    > Compiler Green Hills C++, Version 4.0.6
    >
    > --- foo.cpp ---
    > struct A
    > {
    > };
    >
    > struct B
    > {
    > B() {}
    > };
    >
    > struct C
    > {
    > virtual foo() {}
    > };
    >
    > int main ()
    > {
    > A a;
    > B b;
    > C c;
    > return 0;
    > }
    > ---------------
    >

    [snip assembly]
    >
    > We can see that:
    > * explicit default constructor B::B() is called while building the b
    > object;
    > * implicit default constructor C::C() is called while building the c
    > object;
    > * implicit default constructor A::A() is NOT called while building the
    > a object.
    >
    > So, A::A() is not called. Why?


    C++ only defines observable behaviour so there is no need to call it.
    Also as B's constructor does nothing, there's no reason to call it
    either. The reason it happens is either that you have disables
    optimisations (or use a compiler that is unable to optimise that
    aspect).

    Peter
    peter koch, Aug 9, 2006
    #2
    1. Advertising

  3. Alex Vinokur

    Alex Vinokur Guest

    "peter koch" <> wrote in message news:...
    >
    > Alex Vinokur wrote:
    > > Compiler Green Hills C++, Version 4.0.6
    > >
    > > --- foo.cpp ---
    > > struct A
    > > {
    > > };
    > >
    > > struct B
    > > {
    > > B() {}
    > > };
    > >
    > > struct C
    > > {
    > > virtual foo() {}
    > > };
    > >
    > > int main ()
    > > {
    > > A a;
    > > B b;
    > > C c;
    > > return 0;
    > > }
    > > ---------------
    > >

    > [snip assembly]
    > >
    > > We can see that:
    > > * explicit default constructor B::B() is called while building the b
    > > object;
    > > * implicit default constructor C::C() is called while building the c
    > > object;
    > > * implicit default constructor A::A() is NOT called while building the
    > > a object.
    > >
    > > So, A::A() is not called. Why?

    >
    > C++ only defines observable behaviour so there is no need to call it.
    > Also as B's constructor does nothing, there's no reason to call it
    > either.

    [snip]

    Why is there a reason to call C's implicit constructor?


    --
    Alex Vinokur
    email: alex DOT vinokur AT gmail DOT com
    http://mathforum.org/library/view/10978.html
    http://sourceforge.net/users/alexvn
    Alex Vinokur, Aug 9, 2006
    #3
  4. Alex Vinokur wrote:
    > "peter koch" <> wrote in message
    > news:...
    >>
    >> Alex Vinokur wrote:
    >>> Compiler Green Hills C++, Version 4.0.6
    >>>
    >>> --- foo.cpp ---
    >>> struct A
    >>> {
    >>> };
    >>>
    >>> struct B
    >>> {
    >>> B() {}
    >>> };
    >>>
    >>> struct C
    >>> {
    >>> virtual foo() {}
    >>> };
    >>>
    >>> int main ()
    >>> {
    >>> A a;
    >>> B b;
    >>> C c;
    >>> return 0;
    >>> }
    >>> ---------------
    >>>

    >> [snip assembly]
    >>>
    >>> We can see that:


    /HOW/ can *we* see it?

    >>> * explicit default constructor B::B() is called while building the b
    >>> object;
    >>> * implicit default constructor C::C() is called while building the c
    >>> object;
    >>> * implicit default constructor A::A() is NOT called while building
    >>> the a object.
    >>>
    >>> So, A::A() is not called. Why?


    Actually I am not sure why you claim anything is done in that program,
    the code is ill-formed since 'C::foo' has no return value type.

    >>
    >> C++ only defines observable behaviour so there is no need to call it.
    >> Also as B's constructor does nothing, there's no reason to call it
    >> either.

    > [snip]
    >
    > Why is there a reason to call C's implicit constructor?


    It is totally implementation-defined, but the compiler is _allowed_ to
    generate any code it needs to do what it has to. Your 'C' *allegedly*
    is polymorphic. The compiler *may* need to do something special to take
    the necessary steps to ensure that the polymorph-ness of 'C' is assured.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Aug 9, 2006
    #4
  5. Alex Vinokur

    Alex Vinokur Guest

    "Victor Bazarov" <> wrote in message news:ebd6d6$d9o$...
    > Alex Vinokur wrote:
    > > "peter koch" <> wrote in message
    > > news:...
    > >>
    > >> Alex Vinokur wrote:
    > >>> Compiler Green Hills C++, Version 4.0.6
    > >>>
    > >>> --- foo.cpp ---
    > >>> struct A
    > >>> {
    > >>> };
    > >>>
    > >>> struct B
    > >>> {
    > >>> B() {}
    > >>> };
    > >>>
    > >>> struct C
    > >>> {
    > >>> virtual foo() {}
    > >>> };
    > >>>
    > >>> int main ()
    > >>> {
    > >>> A a;
    > >>> B b;
    > >>> C c;
    > >>> return 0;
    > >>> }
    > >>> ---------------
    > >>>
    > >> [snip assembly]
    > >>>
    > >>> We can see that:

    >
    > /HOW/ can *we* see it?
    >
    > >>> * explicit default constructor B::B() is called while building the b
    > >>> object;
    > >>> * implicit default constructor C::C() is called while building the c
    > >>> object;
    > >>> * implicit default constructor A::A() is NOT called while building
    > >>> the a object.
    > >>>
    > >>> So, A::A() is not called. Why?

    >
    > Actually I am not sure why you claim anything is done in that program,
    > the code is ill-formed since 'C::foo' has no return value type.


    Of course, it should be as follows:
    struct C
    {
    virtual void foo() {}
    };

    >
    > >>
    > >> C++ only defines observable behaviour so there is no need to call it.
    > >> Also as B's constructor does nothing, there's no reason to call it
    > >> either.

    > > [snip]
    > >
    > > Why is there a reason to call C's implicit constructor?

    >
    > It is totally implementation-defined, but the compiler is _allowed_ to
    > generate any code it needs to do what it has to. Your 'C' *allegedly*
    > is polymorphic. The compiler *may* need to do something special to take
    > the necessary steps to ensure that the polymorph-ness of 'C' is assured.

    [snip]

    Once again about class A.
    (Implicit) A::A() is not called. Is the A::a instance indeed created (without invocation of _any_ constructor)?


    --
    Alex Vinokur
    email: alex DOT vinokur AT gmail DOT com
    http://mathforum.org/library/view/10978.html
    http://sourceforge.net/users/alexvn
    Alex Vinokur, Aug 9, 2006
    #5
  6. Alex Vinokur wrote:
    > [snip]
    >
    > Once again about class A.
    > (Implicit) A::A() is not called.


    Once again... How do you know?

    > Is the A::a instance indeed created
    > (without invocation of _any_ constructor)?


    Probably. That's what "implicit" means. Why do you care?

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Aug 9, 2006
    #6
  7. Alex Vinokur

    Marcus Kwok Guest

    Alex Vinokur <> wrote:
    > Compiler Green Hills C++, Version 4.0.6
    >
    > --- foo.cpp ---
    > struct A
    > {
    > };
    >
    > struct B
    > {
    > B() {}
    > };
    >
    > struct C
    > {
    > virtual foo() {}
    > };
    >
    > int main ()
    > {
    > A a;
    > B b;
    > C c;
    > return 0;
    > }
    > ---------------


    [assembly snipped]

    > We can see that:
    > * implicit default constructor A::A() is NOT called while building the
    > a object.
    >
    > So, A::A() is not called. Why?


    Maybe it's because A is a POD. B is not POD since it has a user-defined
    constructor, and C is not POD since it has a virtual function.

    --
    Marcus Kwok
    Replace 'invalid' with 'net' to reply
    Marcus Kwok, Aug 9, 2006
    #7
  8. Alex Vinokur

    Alex Vinokur Guest

    "Victor Bazarov" <> wrote in message news:ebd9mq$j5g$...
    > Alex Vinokur wrote:
    > > [snip]
    > >
    > > Once again about class A.
    > > (Implicit) A::A() is not called.

    >
    > Once again... How do you know?
    >
    > > Is the A::a instance indeed created
    > > (without invocation of _any_ constructor)?

    >
    > Probably. That's what "implicit" means. Why do you care?

    [snip]

    From http://www.sgi.com/tech/stl/uninitialized_copy.html :
    In C++, the operator new
    * allocates memory for an object
    and
    * then creates an object at that location by calling a constructor.

    For the A::a object:
    * memory has been allocated
    * a constructor has not been called.
    Question. If a constructor is not called, is an object created?

    --
    Alex Vinokur
    email: alex DOT vinokur AT gmail DOT com
    http://mathforum.org/library/view/10978.html
    http://sourceforge.net/users/alexvn
    Alex Vinokur, Aug 10, 2006
    #8
  9. Alex Vinokur wrote:
    > "Victor Bazarov" <> wrote in message
    > news:ebd9mq$j5g$...
    >> Alex Vinokur wrote:
    >>> [snip]
    >>>
    >>> Once again about class A.
    >>> (Implicit) A::A() is not called.

    >>
    >> Once again... How do you know?
    >>
    >>> Is the A::a instance indeed created
    >>> (without invocation of _any_ constructor)?

    >>
    >> Probably. That's what "implicit" means. Why do you care?

    > [snip]
    >
    > From http://www.sgi.com/tech/stl/uninitialized_copy.html :
    > In C++, the operator new
    > * allocates memory for an object
    > and
    > * then creates an object at that location by calling a constructor.
    >
    > For the A::a object:
    > * memory has been allocated
    > * a constructor has not been called.
    > Question. If a constructor is not called, is an object created?


    It's an *imaginary* constructor. It has nothing to do so it doesn't
    really exist. So, you're constructing without really calling it, or
    destroying without really calling a d-tor (although you could). Just
    like making a pseudo-destructor call is not really destroying an object
    or using the special syntax <type-id>() is not really calling the
    default c-tor either:

    int a = 42;
    a = int(); // a is now 0
    a::~int(); // pseudo-destructor call

    especially when your object is of POD type...

    You shouldn't take words on SGI's web site seriously, either.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Aug 10, 2006
    #9
  10. Alex Vinokur schrieb:
    > "Victor Bazarov" <> wrote in message news:ebd9mq$j5g$...
    >> Alex Vinokur wrote:
    >>> [snip]
    >>>
    >>> Once again about class A.
    >>> (Implicit) A::A() is not called.

    >> Once again... How do you know?
    >>
    >>> Is the A::a instance indeed created
    >>> (without invocation of _any_ constructor)?

    >> Probably. That's what "implicit" means. Why do you care?

    > [snip]
    >
    > From http://www.sgi.com/tech/stl/uninitialized_copy.html :
    > In C++, the operator new
    > * allocates memory for an object
    > and
    > * then creates an object at that location by calling a constructor.
    >
    > For the A::a object:
    > * memory has been allocated
    > * a constructor has not been called.
    > Question. If a constructor is not called, is an object created?


    Yuo didn't use operator new in the example, did you?

    However, struct A has no objects, no data in it, so why should a
    constructor be called?

    The C++ standard defines behaviour. If a call to a constructor has the
    same effect as not to call it, then the compiler doesn't _have_ to call
    it, but it can, as struct B demonstrates.

    --
    Thomas
    Thomas J. Gritzan, Aug 10, 2006
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Aire
    Replies:
    3
    Views:
    457
    Mike Wahler
    Jan 25, 2004
  2. Replies:
    9
    Views:
    949
    Alf P. Steinbach
    Mar 6, 2006
  3. Generic Usenet Account
    Replies:
    10
    Views:
    2,204
  4. albert kao
    Replies:
    1
    Views:
    851
    Eric Sosman
    Jan 21, 2011
  5. jagan
    Replies:
    2
    Views:
    772
    Jorgen Grahn
    Dec 11, 2011
Loading...

Share This Page