4 ms·
From the tutorial: #include <stdio.h> int main() { int i; return i; } the behaviour is undefined because i’s value is indet
by WalterBright 10mo ago
From the tutorial:
#include <stdio.h>
int main()
{
int i;
return i;
}
the behaviour is undefined because i’s value is indeterminate.
D solves that problem by initializing variables with the default initializer (in this case 0) if there is not an explicit initializer.
That was in the first version of D because I have spent days tracking down an erratic bug that turned out to be an uninitialized variable.
- WalterBright 10mo agoBTW, when D compiles C code, it will default initialize the C variables, too.
- dadoum 10mo agoI think that the right thing to do is to error out though. When the behaviour of some code cannot be guaranteed, that code should just be ruled out imo. Manually initializing a variable generally doesn't clutter the code, arguably it's making it clearer.
- Alifatisk 10mo agoDart does this, you can mark a variable as "late" which tells the compiler that you know for certain the variable will be written too before read. If something reads the variable before it's initialized, then the runtime will error. Maybe even on compile time if it can be caught, I am not certain.
- pseudohadamard 10mo agoPretty much any existing C compiler will also solve that problem by telling you that it's uninitialized.
- WalterBright 10mo agoI tried: int test() { int i; return i; } using clang on my Mac mini, and: clang -c test.c and it compiled without complaint.
- pseudohadamard 10mo agoI always build with -Wall so I'm used to seeing the warning: > clang -Wall test.c test.c:4:16: warning: variable 'i' is uninitialized when used here [-Wuninitialized] 4 | return i; | ^ test.c:3:14: note: initialize the variable 'i' to silence this warning 3 | int i; | ^ | = 0 1 warning generated. For the oldest compiler I have access to, VC++ 6.0 from 1998: warning C4700: uninitialized local variable 'i' used
- WalterBright 10mo agoThe trouble with warnings is every compiler has a different set of warnings. It balkanizes the language. Many D features are the result of cherry picking warnings from various compilers and making them standard features.
- 1718627440 10mo ago> It balkanizes the language. Not really, as C has had even more diverse implementations per-standardization. I would say the situation is now, much less diverse under the rule of GCC and Clang. (Yeah MSVC also exists.)
- WalterBright 10mo agoEvery switch that changes the language semantics creates a separate language. If you have n such switches, your compiler is supporting n x n languages. I've also had troubles writing portable C code with all warnings enabled as different compilers contradicted each other on what was acceptable. I tried pretty hard to make D a warning-less language, but still some crept in grump grump. Have fun with this one: for (int i = 0; i < end; ++i); foo(i); One of the best programmers I know came up to me with this loop and told me my C compiler was broken because the loop was only executed once. I pointed at the ; and you can guess the rest. I added a warning for that in the C compiler, and for D simply disallowed it. I've noticed that some C compilers have since added a warning for that as well. The C folks should just make it illegal. I've also fixed printf in D so that: char* p; printf("%d\n", p); gives an error message, and the right format to use for `p`. It was a little thing, but it sure found a lot of incorrect formats in my code.
- rwmj 10mo agoYou can do that in GCC now (-ftrivial-auto-var-init=zero). There are a few projects that decided to enable this by default.
- WalterBright 10mo agoShould add it to the C standard.