Implicit default constructor is not called

A

Alex Vinokur

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
 
P

peter koch

Alex said:
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
 
A

Alex Vinokur

peter koch said:
Alex said:
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?
 
V

Victor Bazarov

/HOW/ can *we* see it?

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
 
A

Alex Vinokur

Victor Bazarov said:
Alex said:
peter koch said:
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?

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)?
 
V

Victor Bazarov

Alex said:
[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
 
M

Marcus Kwok

Alex Vinokur said:
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.
 
A

Alex Vinokur

Victor Bazarov said:
Alex said:
[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?
 
V

Victor Bazarov

Alex said:
Victor Bazarov said:
Alex said:
[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
 
T

Thomas J. Gritzan

Alex said:
Victor Bazarov said:
Alex said:
[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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top