I'm fiddeling aroung with Swifts UnsafeMutablePointer
type, and came across the dealloc(num:)
method.
Now my knowledge on how pointers work in Swift are all based on assumptions, which have seemed to hold up until now though:
- the
UnsafeMutablePointer<Type>.alloc(num:)
method askes the operating system for a chunk of memory, which is large enough to holdnum
values of the givenType
- the
initialize(newValue:)
method places thenewValue
at the memory address pointed at by the pointer - the
memory
property allows me to read and write the value stored at the pointer's address
Now here the description of the dealloc(num:)
method says:
Deallocate
num
objects.num number of objects to deallocate. Should match exactly the value that was passed to
alloc()
(partial deallocations are not possible).
So I assume what dealloc(num:)
does is to tell the operating system, that it can have the memory chunk back, that starts at the address pointed to by the pointer and ends at the location: (address + sizeof(Type) ⋅ num).
So why are partial decallocations not possible?
Would the problem be, that some of the memory would potentially stay allocated for ever (as long as the app runs)?