[Mono-dev] Mono's handling of Structs

Jonathan Gilbert 2a5gjx302 at sneakemail.com
Wed Nov 23 00:49:36 EST 2005


At 08:40 PM 22/11/2005 -0800, Bill Six wrote:
[snip]
>Is this a bug in the mono compiler?  Should it
>automatically know that a declaration of a struct
>implies calling the default constructor? 
[snip]

Structs do not have default constructors. As members of classes, variables
of value types get initialized to whatever interpretation "all bits 0" has
-- equivalent to "memset(mystruct, 0, sizeof(mystruct));" in C/C++. As
local variables, the individual members of structs are (or should be)
considered independently for initialized-ness, but the struct as a whole
cannot be used (e.g. passed as a parameter or assigned elsewhere) until all
of those members have been written to. As with any other local variable, in
the absence of an initializer, the value is "undefined" prior to
assignment, and it is an error to try to use it.

To summarize, within code, "Point pt;" allocates the memory for pt, but
does not initialize it in any way. "Point pt = new Point();" sets all
members of pt to 0 or null, depending on the data type. Outside of code, as
a class member, "Point pt;" gets initialized to zeroes as part of the class
instance initialization, and not because of anything special tied to the
value type "Point".

Jonathan Gilbert




More information about the Mono-devel-list mailing list