swift计算使用内存大小,MemoryLayout使用

MemoryLayout使用

var age = 18

MemoryLayout<Int>.size // 实际用到的大小
MemoryLayout<Int>.stride //分配的内存大小
MemoryLayout<Int>.alignment //对齐参数

MemoryLayout.size(ofValue: age)
MemoryLayout.stride(ofValue: age)
MemoryLayout.alignment(ofValue: age)

结果:
image

enum Password{
    case num(Int, Int, Int, Int)
    case other
}

MemoryLayout<Password>.size
MemoryLayout<Password>.stride
MemoryLayout<Password>.alignment

结果:

image

posted @ 2022-03-14 11:35  基地您  阅读(244)  评论(0编辑  收藏  举报