473,412 Members | 2,087 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,412 software developers and data experts.

Implicit default constructor is not called

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

Aug 9 '06 #1
9 3525

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

Aug 9 '06 #2

"peter koch" <pe***************@gmail.comwrote in message news:11**********************@m73g2000cwd.googlegr oups.com...
>
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

Aug 9 '06 #3
Alex Vinokur wrote:
"peter koch" <pe***************@gmail.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
>>
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
Aug 9 '06 #4

"Victor Bazarov" <v.********@comAcast.netwrote in message news:eb**********@news.datemas.de...
Alex Vinokur wrote:
"peter koch" <pe***************@gmail.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
>
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

Aug 9 '06 #5
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
Aug 9 '06 #6
Alex Vinokur <al****@users.sourceforge.netwrote:
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
Aug 9 '06 #7

"Victor Bazarov" <v.********@comAcast.netwrote in message news:eb**********@news.datemas.de...
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

Aug 10 '06 #8
Alex Vinokur wrote:
"Victor Bazarov" <v.********@comAcast.netwrote in message
news:eb**********@news.datemas.de...
>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
Aug 10 '06 #9
Alex Vinokur schrieb:
"Victor Bazarov" <v.********@comAcast.netwrote in message news:eb**********@news.datemas.de...
>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
Aug 10 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: A | last post by:
Hi, A default copy constructor is created for you when you don't specify one yourself. In such case, the default copy constructor will simply do a bitwise copy for primitives (including...
9
by: sb | last post by:
If there is at least one user-defined constructor, no constructors are implicitly declared. struct my_vec : public vector<int> { double foo; my_vec(size_t n) : vector<int>(2*n) {} // oops, no...
5
by: JezB | last post by:
Is there any way in a generic class object to implicitly get a reference to the class that instantiated that object ? Obviously I can pass it a reference to store but it would be nice if there was...
25
by: Chad Z. Hower aka Kudzu | last post by:
I have defined some implicit convertors so that I can do: MyStruct x = 4; The implicit convert the 4 into MyStruct. But its essentially a copy constructor and thus if I had: MyStruct x;...
36
by: Chad Z. Hower aka Kudzu | last post by:
I have an implicit conversion set up in an assembly from a Stream to something else. In C#, it works. In VB it does not. Does VB support implicit conversions? And if so any idea why it would work...
10
by: Joel | last post by:
Is it true that if we don't specify a default constructor for our class, then the C# compiler provides us with its own that zeroes (or assigns default values) to the data members? I wrote a...
74
by: Zytan | last post by:
I have a struct constructor to initialize all of my private (or public readonly) fields. There still exists the default constructor that sets them all to zero. Is there a way to remove the...
23
by: Jess | last post by:
Hello, I understand the default-initialization happens if we don't initialize an object explicitly. I think for an object of a class type, the value is determined by the constructor, and for...
4
by: Jess | last post by:
Hello, I tried several books to find out the details of object initialization. Unfortunately, I'm still confused by two specific concepts, namely default-initialization and...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.