[swift-users] What is "binding" memory?

Dave Abrahams dabrahams at apple.com
Wed Nov 2 14:58:18 CDT 2016


on Tue Nov 01 2016, Andrew Trick <swift-users-AT-swift.org> wrote:

>> On Nov 1, 2016, at 11:55 AM, Manfred Schubert via swift-users <swift-users at swift.org> wrote:
>> 
>> The "UnsafeRawPointer Migration" guide talks about "binding memory
>> to a type“ as if that was a well known term. I have never heard of
>> it yet though, and googling it returns no relevant results. I do not
>
>> understand what binding memory is supposed to do.
>> 
>> The migration guide says "Binding uninitialized memory to a type
>> prepares the memory to store values of that type“, but clearly raw
>> memory does not need to be prepared (and cannot be) to hold any
>> arbitrary type and value.
>> 
>> So what is this for, what does it actually do, and to whom is it done (the raw pointer, or the
> typed pointer which is returned, or the raw memory)?
>> 
>> 
>> Manfred
>
> Hi Manfred,
>
> At the top of the migration guide is a link to the memory model explanation:
> https://github.com/apple/swift-evolution/blob/master/proposals/0107-unsaferawpointer.md#memory-model-explanation
>
> "A memory location's bound type is an abstract, dynamic property of the memory used to formalize
> type safety.”
>
> I’m not sure I like the “prepares the memory” language myself. Binding
> memory communicates to the compiler that the memory locations are safe
> for typed access. Nothing happens at runtime--until someone writes a
> type safety sanitizer. 

Well, that's a slight overstatement IMO.  Sanitizers aside, the main
reason for these binding operations is that if you leave them out,
something different *will* happen at runtime... something that will make
your code do the wrong thing.

What I would say is that binding the memory has no immediate runtime
cost... but it's absolutely required if you want your program to behave
(and sometimes behaving correctly is a little slower than misbehaving).

> It affects the abstract state of the memory location, independent of
> the pointer variable used to access that memory. Binding memory
> returns a typed pointer for convenience and clarity, but there’s
> nothing special about that particular pointer value.
>
> Initialized memory is always bound to some type. A rawpointer can be
> used to access that memory without knowing its bound type.
>
> -Andy
> _______________________________________________
> swift-users mailing list
> swift-users at swift.org
> https://lists.swift.org/mailman/listinfo/swift-users

-- 
-Dave



More information about the swift-users mailing list