[Issue 3449] const and invariant struct members do not behave according to spec

d-bugmail at puremagic.com d-bugmail at puremagic.com
Mon Jan 23 01:37:30 PST 2012


http://d.puremagic.com/issues/show_bug.cgi?id=3449



--- Comment #9 from Walter Bright <bugzilla at digitalmars.com> 2012-01-23 01:37:27 PST ---
(In reply to comment #4)
> More strangeness:  If you don't explicitly provide an initial value for
> const/immutable members, they do contribute to the size of the struct.
>     struct Foo { const int i; }
>     writeln(Foo.sizeof); // Prints 4
>     struct Bar { const int i = 123; }
>     writeln(Bar.sizeof); // Prints 1

This is as designed. A const field without an initializer can be initialized by
a constructor. A const field with an initializer does not need any per-instance
storage, and becomes a static member.

> I suspect that this bug could cause unexpected memory corruption when such
> structs are, for instance, passed to C functions -- especially when the
> behaviour depends on such a small detail.

It is not a bug, it is as designed. (const in D and C are different, and
conflating the two will cause problems anyway)

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list