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:
vladimirn@multiconn.com
[color=blue]
> I just notice some strange behaviors on the MS C++ compiler regarding[/color]
struct[color=blue]
> 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[/color]
struct[color=blue]
> 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[/color]
init[color=blue]
> 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[/color]
local[color=blue]
> 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[/color]
but[color=blue]
> 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[/color]
default[color=blue]
> constructor and when dose not (and expect for one defined).[/color]