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

Manfred Schubert dev at schubert-it.com
Thu Nov 3 17:58:58 CDT 2016


Am 03.11.2016 um 15:41 schrieb Rien <Rien at Balancingrock.nl>:
> 
> Ah, but that is not the case.
> 
> It is important to differentiate between the “gateway” to the memory and the memory area itself.
> Different programming languages/compilers have different approaches, but I believe that Swift allocates a struct for every gateway.
> widePtr and narrowPtr are two different gateways. They refer to different struct's. But the struct for each of them refers to the same memory area.

When you have a look at the Swift memory model explanation

https://github.com/apple/swift-evolution/blob/master/proposals/0107-unsaferawpointer.md#memory-model-explanation

it looks like memory can only be bound to one type at a time. In particular in the third example where a pointer of type T is used to initialize memory which is bound to type U, it says that the behavior is undefined.

There is also withMemoryRebound(to:capacity:) which binds memory to another type, executes the code that accesses the memory as this type in a closure, and the restores the old type binding.

That makes me think that it is not allowed to have multiple „gateways“ to the same memory area at the same time.


Manfred


More information about the swift-users mailing list