On Tue, 01 Sep 2009 07:06:25 -0400, Max Samukha <spam...@d-coding.com>
wrote:
bearophile wrote:
A question about D1 specs (that may be useful for LDC).
In the following code there's anarray of structs S. Is it OK, according
to
D1 specs, to not initialize the memory of this array if the compiler
sees
that all fields of S have a void init?
struct S { double x = void, y = void; }
void main() {
auto a = new S[1000];
}
Bye,
bearophile
That's an interesting question. The compiler does set void-initialized
member variables to zeros:
struct S { double x = void, y = void; }
void main() {
S s; // x and y are initialized to 0
}
I think this is a bug. Such members should be left uninitialized or
initialized to the default initializer for the type (NaN in this case) or
void initialization should be disallowed for member variables.
An initialized double is NaN, so I don't think the compiler is
initializing those variables.
Your test is not valid, you need to fill the memory with something to see
if the code doesn't change it.
Try this:
foo()
{
S s;
// display s's members here
s.x = s.y = 555;
}
void main()
{
foo();
foo();
}
This will make the compiler reuse the stack memory, which should be
initialized to 555 on the second run.
But I think in answer to bearophile's question, you probably need to
perform a similar test for allocation, getting the GC to allocate the same
memory twice. Even so, the GC propbably zeros out all memory before
returning it, to cut down on false pointers being detected by the GC on a
collection cycle. I'm not knowledgable with the details of the GC, so I
could be completely wrong.
At the very least, the compiler needs to initialize the memory in the
block that was returned but not requested, to avoid false pointers.
-Steve