swift开发知识收集

汇编窥探Swift底层(二):枚举

2020-02-25  本文已影响0人  冰风v落叶

窥探枚举的内存

enum TestMemory{
    case test1(Int,Int,Int)
    case test2(Int)
    case test3(Bool)
    case test4
}

初始化一个枚举,关联值是4,5,6
var memory = TestMemory.test1(4, 5, 6)

这句代码是打印变量memory的内存地址,打印出来是0x0000000100008208
print(Mems.ptr(ofVal: &memory))  
memory的内存
查看memory实际占用了多少个字节
print(MemoryLayout<TestMemory>.size)        实际占用了25个字节

查看memory分配了占用了多少个字节
print(MemoryLayout<TestMemory>.stride)      分配了32个字节

查看TestMemory的字节对齐数
print(MemoryLayout<TestMemory>.alignment)   字节对齐是8
枚举不变,只是将memory变成test2
var memory = TestMemory.test2(7)
print(Mems.ptr(ofVal: &memory))
test2时的内存
memory = .test3(true)
print(Mems.ptr(ofVal: &memory)) 打印memory的内存地址
memory = .test3(true)时的内存
memory = .test4
print(Mems.ptr(ofVal: &memory)) 打印memory的内存地址
memory = .test4时的内存.png
上一篇 下一篇

猜你喜欢

热点阅读