Safe cast of arrays
Steven Schveighoffer via Digitalmars-d
digitalmars-d at puremagic.com
Wed Feb 10 19:39:20 PST 2016
On 2/10/16 5:49 PM, Chris Wright wrote:
> On Wed, 10 Feb 2016 21:40:21 +0000, Iakh wrote:
>
>> On Wednesday, 10 February 2016 at 20:14:29 UTC, Chris Wright wrote:
>>> @safe protects you from segmentation faults and reading and writing
>>> outside an allocated segment of memory. With array casts, @safety is
>>> assured
>>
>> Yes, @safe protects from direct cast to/from ref types but there still
>> is a trick with T[] -> void[] -> T2[] cast:
>>
>> So no safety in this world.
>
> Okay, that's a problem.
>
> It should always be safe to cast from void[] to immutable(T)[] where T
> doesn't contain pointers.
>
> I didn't see a bug for this, so I'm filing it.
>
I think casting a mutable array to any array type is a recipe for memory
issues, no matter what is in the elements. Remember that you are casting
a reference that still has a mutable pointer to it.
@safe should start from a very cautious and overtightened state, and
then we loosen it as we find issues.
As it was done, it has holes, and so when we fix things, code breaks.
-Steve
More information about the Digitalmars-d
mailing list