Segfault when casting array of Interface types
via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Tue Sep 16 01:49:02 PDT 2014
On Tuesday, 16 September 2014 at 08:39:43 UTC, Marc Schütz wrote:
> AFACIS there's nothing wrong with his use of casting. It's fine
> here, because `I` is a base type of `C` and `D`. If it weren't
> for the arrays, the cast wouldn't even be necessary. I think
> it's a bug.
Correction:
AFAIK casting between interfaces and classes needs to adjust the
underlying pointer. Therefore, when casting an array, the
compiler would have to do that with the entire array, which
cannot be copied without allocating memory (and mustn't be
modified in-place for consistency reasons). This means that the
cast is instead a pure reinterpret cast (repainting).
Whether the compiler should accept that or not is a different
question. I guess it should, because if it doesn't, there
wouldn't be an easy way to achieve a reinterpret cast (only via
an intermediate cast to `void*`, which is clumsy).
Anyway, using `std.conv.to` is the way to go here (if you don't
require that last bit of performance), because it is safer in
general (also checks for overflows and the like, for example).
More information about the Digitalmars-d-learn
mailing list