473,473 Members | 2,147 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

C++ Compiler behavior regerding struct constructors

Hi everyone,
I just notice some strange behaviors on the MS C++ compiler regarding struct
default constructor, see the example bellow:
struct MyStruct
{
int a[5];
};
class MyClass
{
public:
MyClass();
~MyClass();
protected:
MyStruct m_MyStruct;
};
//#1:
MyClass::MyClass() //The default ctor for the class does not init the struct
with NULL(maybe there is no default ctor for struct yet?)
{
}
//#2 if I define the class ctor like this:
MyClass::MyClass() : m_MyStruct() //The default ctor for the class does init
the struct with NULL(call m_MyStruct() default ctor, now there is one!)
{
}
//#3 or like this:
MyClass::MyClass()
{
m_MyStruct = MyStruct(); // dose init the struct with NULL using a local
stack var init with NULL
}
//Now if I define the struct like this:
struct MyStruct
{
MyStruct()
{
int i;
for(i=0;i<5;i++)
a[i] = NULL;
}
int a[5];
};
//#1 will init the struct with NULL because uses the defined struct ctor but
generate almost the same bin (asm) code as case #2 and #3 (the init stack
var) without the struct ctor defined!
I guess I don't understand when the compiler knows about the struct default
constructor and when dose not (and expect for one defined).

Thank you
Karl M
Nov 17 '05 #1
3 1888
Karl M wrote:
Hi everyone,
I just notice some strange behaviors on the MS C++ compiler regarding
struct default constructor
You're just not used to the initialization rules for C++ (which are arcane
and obscure).

When you write...

int a[5];

....the contents of a[] are undefined.

Likewise...

struct Z
{
int a[5];
};

Z z;

....the contents of z are undefined.

8.5/9 of the C++ standard says

<quote>
If no initializer is specified for an object, and the object is of (possibly
cv-qualified) nonPOD class type (or array thereof), the object shall be
default-initialized; if the object is of const-qualified type, the
underlying class type shall have a user-declared default constructor.
Otherwise, if no initializer is specified for an object, the object and its
sub-objects, if any, have an indeterminate initial value; if the object or
any of its sub-objects are of const-qualified type, the program is
ill-formed.
</quote>

You're falling afoul of the "Otheriwse..." part of that quotation.
struct MyStruct
{
int a[5];
};
class MyClass
{
public:
MyClass();
~MyClass();
protected:
MyStruct m_MyStruct;
};
//#1:
MyClass::MyClass() //The default ctor for the class does not init the
struct with NULL(maybe there is no default ctor for struct yet?)
{
Here m_MyStruct has undefined content because MyStruct is a POD (which
implies no user defined constructors) so it's not initialized.
}
//#2 if I define the class ctor like this:
MyClass::MyClass() : m_MyStruct() //The default ctor for the class
does init the struct with NULL(call m_MyStruct() default ctor, now
there is one!) {
Correct. Since you've forced the creation of a default contructor for
MyStruct, that default constructor zero-fills the memory.
}
//#3 or like this:
MyClass::MyClass()
{
m_MyStruct = MyStruct(); // dose init the struct with NULL using a
local stack var init with NULL
Likewise, you've forced the creation (and calling) of a default constructor
for MyStruct() which zero-fills the memory.
}
//Now if I define the struct like this:
struct MyStruct
{
MyStruct()
{
int i;
for(i=0;i<5;i++)
a[i] = NULL;
}
int a[5];
};
This is equivalent to the default constructor that the compiler will
generate for you, so it shouldn't be surprising that the results are nearly
identical.
//#1 will init the struct with NULL because uses the defined struct
ctor but generate almost the same bin (asm) code as case #2 and #3
(the init stack var) without the struct ctor defined!
I guess I don't understand when the compiler knows about the struct
default constructor and when dose not (and expect for one defined).


It always knows about it, your example #1 simply doesn't force the compiler
to explicitly create a default constructor. Getting back to the quote from
8.5 of the standard - once you force a struct to have a constructor, it's no
longer a POD, so the code falls under the first sentence of that passage -
the object is default-initialized.

The best advice is: if you need initialization, write a constructor.

HTH

-cd
Nov 17 '05 #2
C++ guarantees zero initialization for static variables of primitive type,
or of primitive structure, or of array of primitive types (structures) at
program start up.
Some heaps provide garantee for zero allocated memory.
Managed stack also provides zero initialized stack frame garantee.
In all other cases developer should take care of memory initialization.

I believe it's an extension that:

MyStruct m_MyStruct;

and

MyStruct m_MyStruct = MyStruct();

generate different code.
--
Vladimir Nesterovsky
e-mail: vl*******@multiconn.com
I just notice some strange behaviors on the MS C++ compiler regarding struct default constructor, see the example bellow:
struct MyStruct
{
int a[5];
};
class MyClass
{
public:
MyClass();
~MyClass();
protected:
MyStruct m_MyStruct;
};
//#1:
MyClass::MyClass() //The default ctor for the class does not init the struct with NULL(maybe there is no default ctor for struct yet?)
{
}
//#2 if I define the class ctor like this:
MyClass::MyClass() : m_MyStruct() //The default ctor for the class does init the struct with NULL(call m_MyStruct() default ctor, now there is one!)
{
}
//#3 or like this:
MyClass::MyClass()
{
m_MyStruct = MyStruct(); // dose init the struct with NULL using a local stack var init with NULL
}
//Now if I define the struct like this:
struct MyStruct
{
MyStruct()
{
int i;
for(i=0;i<5;i++)
a[i] = NULL;
}
int a[5];
};
//#1 will init the struct with NULL because uses the defined struct ctor but generate almost the same bin (asm) code as case #2 and #3 (the init stack
var) without the struct ctor defined!
I guess I don't understand when the compiler knows about the struct default constructor and when dose not (and expect for one defined).

Nov 17 '05 #3
Daniel,
Thank you for the insides of c++ standards.
I was only interseted if there is a way to call the "compiler default"
struct ctor in the class ctor body.
If I define a struct ctor then it is automatically embeded into the class
ctor underlying header (don't want that).

Regards,
Karl
Nov 17 '05 #4

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

Similar topics

13
by: Generic Usenet Account | last post by:
Compile the following snippet of code and run it. If the program spits out bat:bat instead of bat:zat, what would you say? Would you say that the compiler has a problem, or would you lay the...
29
by: junky_fellow | last post by:
Consider the following piece of code: struct junk { int i_val; int i_val1; char c_val; }; int main(void) {
1
by: John Madsen | last post by:
This bug is easier to just show than to explain I think... namespace M { template <class T> struct A { void f(int a = T::foo()) { } // line 5 }; } namespace N { struct B {
12
by: Andrew Schepler | last post by:
When compiled with Visual C++ .NET 2003 (only), the program below aborts as though no matching catch clause is present. If the copy constructor of A is made public, it successfully catches the...
29
by: Ark | last post by:
A function int foo(struct T *x) { return (x+1)-x; } should always return a 1, no matter how T is defined. (And int could be replaced with ptrdiff_t for you pedants.) For one thing, it was...
6
by: JohnQ | last post by:
I like, non-copyable, non-assignable and, most often, non-default-constructable also, as a starting point for class design: class SomeClass { SomeClass(); // disallow default construction...
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...
43
by: JohnQ | last post by:
Are a default constructor, destructor, copy constructor and assignment operator generated by the compiler for a struct if they are not explicitely defined? I think the answer is yes, because...
1
by: jason.cipriani | last post by:
Here is an example with 3 files, containing a template structure and also a template function. The header A.h declares a template structure A with a default (i.e. for any template parameter),...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
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...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.