Re: C++ Compiler behavior regerding struct constructors
From: Vladimir Nesterovsky (vladimir_at_nesterovsky-bros.com)
Date: 12/19/04
- Next message: Peter Schmitz: "RE: [ASAP] Stack corruption"
- Previous message: Carl Daniel [VC++ MVP]: "Re: C++ Compiler behavior regarding struct constructors"
- In reply to: Karl M: "C++ Compiler behavior regerding struct constructors"
- Messages sorted by: [ date ] [ thread ]
Date: Sun, 19 Dec 2004 18:33:33 +0200
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
> 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).
- Next message: Peter Schmitz: "RE: [ASAP] Stack corruption"
- Previous message: Carl Daniel [VC++ MVP]: "Re: C++ Compiler behavior regarding struct constructors"
- In reply to: Karl M: "C++ Compiler behavior regerding struct constructors"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|