Sometimes, the array TypeInfo.init() returns has a length, but ptr is null. Accessing an element of such an array causes segfaults. This can't be right. TypeInfo.init() should just return an empty array. Example follows. This outputs "ptr=0000 length=4". import std.stdio; struct X { int y; } void main() { TypeInfo ti = typeid(X); writefln("ptr=%s length=%s", ti.init.ptr, ti.init.length); }
This is really a documentation issue. The init() is correct, as I discovered when fixing a recent bug. Essentially, when the ptr is null, but the length is non-zero, the meaning is that the init value for that type is init().length bytes of zero. This cuts down on having to store an array of zeros in the binary. The documentation for init() says: "Return default initializer, null if default initialize to 0" Which seems to indicate init() should return null if default initializer should be zero. But a null array has length == 0. I think the right solution to this is to change the documentation: "Return default initializer. If the type should be initialized to all zeros, an array with a null ptr and a length equal to the type size will be returned" Sound good?
This is deliberate, see the typinf.c code: // void[] init; dtsize_t(pdt, sd->structsize); // init.length if (sd->zeroInit) dtsize_t(pdt, 0); // NULL for 0 initialization else dtxoff(pdt, sd->toInitializer(), 0, TYnptr); // init.ptr I'll fix the documentation.
https://github.com/D-Programming-Language/druntime/commit/a771c11bfe357ac2c6c87e96b39e8986286222f5 https://github.com/D-Programming-Language/phobos/commit/50a73bef9ac0e6f8bbc713c1ded85a8254175feb
AltStyle によって変換されたページ (->オリジナル) / アドレス: モード: デフォルト 音声ブラウザ ルビ付き 配色反転 文字拡大 モバイル