3 ms·
My longest one was an uninitialized declaration of a local variable, which acquired ever-changing values. This is why D, by default, initializes all variables.
by WalterBright 2y ago
My longest one was an uninitialized declaration of a local variable, which acquired ever-changing values.
This is why D, by default, initializes all variables. Note that the optimizer removes dead assignments, so this is runtime cost-free. D's implementation of C, ImportC, also default initializes all locals. Why let that stupid C bug continue?
Another that repeatedly bit me was adding a field, and neglecting to add initialization of it to all the constructors.
This is why D guarantees that all fields are initialized.
- hinkley 2y agoThe first bug I remember writing was making native calls in Java to process data. I didn’t understand why in the examples they kept rerunning the handle dereference in every loop. If native code calls back into Java, and the GC kicks in, all the objects the native code can see can be compacted and moved. So my implementation worked fine for all of the smaller test fixtures, and blew up half the time with the largest. Because I skipped a line to make it “go faster”. I finally realized I was seeing raw Java objects in the middle of my “array” and changing the value of final fields into illegal pairings which blew everything the fuck up.
- ckocagil 2y agoValgrind didn't catch it?
- lttlrck 2y agoMay have been impractical to run valgrind eg embedded. A decent compiler should catch this anyway? -Wall?
- WalterBright 2y agoTesting all code paths means all code paths must be available to the compiler. This is not always practical.
- WalterBright 2y agoIt was before valgrind. Besides, valgrind isn't always available, it requires all code paths to be tested, and it can be really slow making it impractical for some code. Default initialization, on the other hand, gives 100% coverage. Experience with it in D is a satisfying success.