If a function returns an UnsafeMutablePointer is it our responsibility to destroy and dealloc?

后端 未结 1 1219
予麋鹿
予麋鹿 2021-02-07 07:39

For example if I were to write this code:

var t = time_t()
time(&t)
let x = localtime(&t) // returns UnsafeMutablePointer
        
println("         


        
1条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-07 08:05

    From Swift library UnsafeMutablePointer

    A pointer to an object of type T. This type provides no automated memory management, and therefore the user must take care to allocate and free memory appropriately.

    The pointer can be in one of the following states:

    • memory is not allocated (for example, pointer is null, or memory has been deallocated previously);
    • memory is allocated, but value has not been initialized;
    • memory is allocated and value is initialized.

    struct UnsafeMutablePointer : RandomAccessIndexType, Hashable, NilLiteralConvertible { /**/}
    

    0 讨论(0)
提交回复
热议问题