swift Pointer 的使用

Pointer Name Unsafe? Write Access? Collection Strideable? Typed?
UnsafeMutablePointer<T> yes yes no yes yes
UnsafePointer<T> yes no no yes yes
UnsafeMutableBufferPointer<T> yes yes yes no yes
UnsafeBufferPointer<T> yes no yes no yes
UnsafeRawPointer yes no no yes no
UnsafeMutableRawPointer yes yes no yes no
UnsafeMutableRawBufferPointer yes yes yes no no
UnsafeRawBufferPointer yes no yes no no
  • unsafe:不安全的
  • Write Access:可写入
  • Collection:像一个容器,可添加数据
  • Strideable:指针可使用 advanced 函数移动
  • Typed:是否需要指定类型(范型)

MemoryLayout
使用MemoryLayout,可以检测某个类型的实际大小(size),内存对齐大小(alignment),以及实际占用的内存大小(步长:stride),其单位均为字节;

public enum MemoryLayout<T> {
    public static var size: Int { get }
    public static var stride: Int { get }
    public static var alignment: Int { get }
    public static func size(ofValue value: T) -> Int
    public static func stride(ofValue value: T) -> Int
    public static func alignment(ofValue value: T) -> Int
}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容