One of the curious aspects of D compared to C or C ++ is that the default variables are initialized according to their type when no assignment value is provided.
int foo() { int o;
Unlike C and C ++, which simply leaves variables with potential garbage, D ensures that garbage is never read from almost all types of variables. However, given this simple, simply hypothetical function, r never read until the value i , and it is certain that the assignment will finally happen.
int foo2(int n) { assert(n > 0 && n < 20); int r; for (int i = n ; ; i+=7) { if (i % 3 == 0) { r = i; break; } } return r; }
- In the case when I am sure that the variable will be defined in the future without a previous reading, will the default initialization still happen, according to the standard?
- Is it known from DMD / GDC compilers to optimize them (as in omitting the default initialization, when this default value is never read from a variable)?
- If none of the above is there, is there a good job with a completely uninitialized variable?
initialization d
E_net4
source share