Why are void[] contents marked as having pointers?

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Sun May 31 13:24:09 PDT 2009


Vladimir Panteleev wrote:
> On Sun, 31 May 2009 22:41:47 +0300, Walter Bright
> <newshound1 at digitalmars.com> wrote:
> 
>> Vladimir Panteleev wrote:
>>> I don't know why it was decided to mark the contents of void[] as
>>>  "might have pointers". It makes no sense! Consider:
>> [...]
>> 
>>> 3) It's very rare in practice that the only pointer to your 
>>> object (which you still plan to access later) to be stored in a 
>>> void[]-allocated array!
>> Rare or common, it still would be a nasty bug lurking to catch
>> someone. The default behavior in D should be to be correct code.
>> Doing potentially unsafe things to improve performance should
>> require extra effort - in this case it would be either using the gc
>> function to mark the memory as not containing pointers, or storing
>> them as ubyte[] instead.
> 
> This isn't about performance, this is about having one thousand casts
> all over my code. It becomes a burden to cast everything to ubyte[]
> when working with abstract binary data. For example, when building a
> MIME multipart message with binary fields, every line needs to have a
> cast in it - when we could have just used the ~= operator to append
> to a void[].

Another alternative would be to allow implicitly casting arrays of any 
type to const(ubyte)[] which is always safe. But I think this is too 
much ado about nothing - you're avoiding the type system to start with, 
so use ubyte, insert a cast, and call it a day. If you have too many 
casts, the problem is most likely elsewhere so that argument I'm not buying.

Andrei



More information about the Digitalmars-d mailing list