Re: C++ Compiler behavior regerding struct constructors

From: Vladimir Nesterovsky (vladimir_at_nesterovsky-bros.com)
Date: 12/19/04


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).


Relevant Pages

  • Mixing C and C++
    ... I have therefore implemented the assignment operator to create 'shallow' copy by default (so this is consistent with the C API so that a pointer to the struct could be passed to a C API function with no adverse effect). ... MyStruct(const MyStruct& ms) ... MyStruct& operator= (const MyStruct& rhs) ... void deepcopy ...
    (comp.lang.c)
  • Re: Question about setjmp on Itanium HPUX.
    ... that was the sound of the alignment of your struct ... jmp_buf and your struct. ... The one value which you are guaranteed is safe for struct mystruct is ... Note that in this case, as in your original code, you need to remember ...
    (comp.lang.c)
  • Re: typedefing a struct
    ... typedef struct mystruct { ... typedef struct mystruct mystruct; ...
    (comp.lang.c)
  • Re: Searching a vector of structs
    ... | If you've got a struct like: ... | int x; ... | because it doesn't know how to compare 10 against a MyStruct. ...
    (alt.comp.lang.learn.c-cpp)
  • PInvoke marshaling a char* inside a struct
    ... I have a C struct that contains a char* and I am tring to use PInvoke ... char* be breaking the marshaling of the reset of the members. ... struct myStruct* st; ...
    (microsoft.public.dotnet.framework.interop)