1

Is there a way to control the address an object will be allocated at in Swift? I know Objective-C has this feature (although it's hidden away in the runtime), and C++ has placement new, but I don't see a way to do it in pure Swift.

Although the UnsafeMutablePointer type may be used to store arbitary objects (using it's initialize... methods), those methods require that the source object already exists, which requires an extra memory allocation and copy.

I've used this technique for doing IPC with memory-mapped files before, but I'd like to write some of my newer side-projects in Swift...

3
  • 1
    This could be what you are looking for: Secure Memory For Swift Objects
    – Martin R
    Commented Dec 3, 2015 at 15:29
  • That looks really close, but the UnsafeMutablePointer<Memory>.initialize...() functions seem to require that the objects they're being initialized with already exist somewhere. I'd like to avoid the extra allocation and copy if possible.
    – alaroldai
    Commented Dec 3, 2015 at 15:54
  • You could write only this specific feature in ObjC and the rest in Swift.
    – Darko
    Commented Dec 3, 2015 at 18:07

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.