Reading about D: few questions
Mr. Anonymous
mailnew4ster at gmail.com
Fri Dec 23 07:25:56 PST 2011
Hi guys!
I'm mostly familiar with C (and a bit of PHP). I've stumbled upon the D
language, and I must say I really like it.
Now I'm reading the "The D Programming Language" book, and I have a
couple of questions:
1. Uninitialized Arrays and GC.
http://dlang.org/memory.html#uninitializedarrays
It's said here ^ that:
"The uninitialized data that is on the stack will get scanned by the
garbage collector looking for any references to allocated memory."
With the given example of: byte[1024] buffer = void;
So does the GC really scan this byte array? Or (sounds more logical to
me) does it scan only reference types?
If the latter is true, I think the example should use some kind of a
pointer array. Also, in this case, I can't see why "Uninitialized data
can be a source of bugs and trouble, even when used correctly."?
If the former is true, then, well, I'll ask more questions.
2. Setting Dynamic Array Length.
http://dlang.org/arrays.html#resize
"A more practical approach would be to minimize the number of resizes"
The solution works but is not as clean as just using array ~= c;
Is there any way (language, runtime, or phobos) to declare an array that
would reallocate memory by chunks, which are multiple of x?
3. const and immutable.
Is there any use for const when defining variables?
As I see it, there is no use for using e.g. const int x;, as it can't be
modified anyway;
So with immutable, const is only good for reference variables that are
initialized to refer to another variable (like a function const ref
parameter).
Am I right?
4. if (&lhs != &rhs)?
std.algorithm has this in it's swap function.
Is it different than if (lhs !is rhs)?
Just wondering.
5. Align attribute.
http://dlang.org/attribute.html#align
struct S {
align(4) byte a; // placed at offset 0
align(4) byte b; // placed at offset 1
}
Explain this please.
6. Array slices manipulation.
a[] += 1; works but a[]++ doesn't.
Not so important but just wondering: why, and is it intended?
7. Anonymous structs.
In C you can write:
struct { int a; } s = {10};
printf("%d\n", s.a);
In D you must declare the struct first:
struct S { int a; };
S s = {10};
writeln(s.a);
Why doesn't D allow anonymous structs?
Best regards.
More information about the Digitalmars-d-learn
mailing list