0

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 hold num values of the given Type
  • the initialize(newValue:) method places the newValue 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)?

Community
  • 1
  • 1
Marcus Rossel
  • 3,196
  • 1
  • 26
  • 41
  • It always depends on how memory allocation is implemented. Usually you have some chained list with the chunks and a shadow list. So when memory is returned the OS can tell if all of the chunk has been given back. If partial deallocation is not allowed (as it seems to be the case here) the OS should croak. – qwerty_so Feb 26 '15 at 20:26
  • Could you elaborate on the "chained list", "shadow list", and what you mean by "the OS should croak"? – Marcus Rossel Feb 26 '15 at 20:31
  • why would you want a partial deallocation? maybe I am a bit behind here but.. why would you want num: alloc != num: dealloc.. I think I wouldn't even have exposed the num: for dealloc :P – Daij-Djan Feb 26 '15 at 20:36
  • the OS should croak means -to me- that it should somehow print an error and/or fire a signal (like it does when malloc doesn't match free) – Daij-Djan Feb 26 '15 at 20:38
  • I don't want `alloc()`'s `num` to be different than `dealloc()`'s. I'm just wondering, why partial deallocations shouldn't be allowed. Say I have allocated 5 elements, and now only need 3. Why shouldn't I be able to deallocate the 2 elements at the end? – Marcus Rossel Feb 26 '15 at 20:43
  • @Marcus: croak has been explained. Memory management is in principle simple. You have one free memory block in the beginning. The first word contains the size and the next word(s) various flags. When memory is allocated the size of the block is decrease, the new start gets the reduced size word/flags and the memory is made available. To keep track of the allocated blocks the OS has a 2nd list where it keeps track of the allocated blocks. So when memory is returned it can check that the user is not trying to cheat. But memory management in reality is by far more complex. – qwerty_so Feb 27 '15 at 08:46

0 Answers0