swift-指针类型Memory Layout的简介与使用

前沿:

swift语言在设计之初是与C语言的API进行了无缝的对接混用,但是在语法上具有很大的差异,swift在默认情况下是内存安全的,这也意味着我们在swift中是禁止使用内存操作的,但是swift并没有对内存的使用进行禁止,因此swift存在了一个特殊的数据类型unsafe

Memory Layout类

MemoryLayout是一个数据结构,用于保存类的内存配置,其组成如下

struct MemoryLayout {
    var size: Int { get } //  连续的内存占用量T,以字节为单位。
    var stride: Int { get } // 存储在连续存储器或存储器中的一个实例的开始到下一个实例的开始的字节数
    var alignment: Int { get } //默认内存对齐方式T,以字节为单位。
    func size(ofValue value: T) -> Int
    func stride(ofValue value: T) -> Int
    func alignment(ofValue value: T) -> Int
}

例如:一个结构体解释MemoryLayout的使用

/*结构体声明*/
struct Point {
    let x: Double
    let y: Double
    let isFilled: Bool
}

/*MemoryLayout的类方法*/
print("Point.size:\(MemoryLayout.size)")
print("Point.stride:\(MemoryLayout.stride)")
print("Point.alignment:\(MemoryLayout.alignment)")

/*MemoryLayout的实例属性*/
let myPoint:Point = Point(x: 10, y: 10, isFilled: true)
print("myPoint.size:\(MemoryLayout.size(ofValue: myPoint))")
print("myPoint.stride:\(MemoryLayout.stride(ofValue: myPoint))")
print("myPoint.alignment:\(MemoryLayout.alignment(ofValue: myPoint))")


打印结果
Point.size:17
Point.stride:24
Point.alignment:8


myPoint.size:17
myPoint.stride:24
myPoint.alignment:8

类、结构体之间使用MemoryLayout的区别

/*结构体*/
struct Point {
    let x: Double
    let y: Double
    let isFilled: Bool
    func sayHelloWord() -> Void {
        print("hello word!!!")
    }
}


/*类*/
class MyClass: NSObject {
    let name:String = "hell"
    let age:Int = 25
    let address:String = "上海市浦东新区"
}

class StudentClass:PersonClass{
    let address:String = "上海市浦东新区"
}

class PersonClass {
    let name:String = "hell"
    let age:Int = 25
}

let myPoint:Point = Point(x: 10, y: 10, isFilled: true)
print("myPoint.size:\(MemoryLayout.size(ofValue: myPoint))")
print("myPoint.stride:\(MemoryLayout.stride(ofValue: myPoint))")
print("myPoint.alignment:\(MemoryLayout.alignment(ofValue: myPoint))")
/*类的MemoryLayout区别*/
let myClass:MyClass = MyClass()
print("myClass.size:\(MemoryLayout.size(ofValue: myClass))")
print("myClass.stride:\(MemoryLayout.stride(ofValue: myClass))")
print("myClass.alignment:\(MemoryLayout.alignment(ofValue: myClass))")
        
        
let student:StudentClass = StudentClass()
print("student.size:\(MemoryLayout.size(ofValue: student))")
print("student.stride:\(MemoryLayout.stride(ofValue: student))")
print("student.alignment:\(MemoryLayout.alignment(ofValue: student))")
        
let person:PersonClass = PersonClass()
print("person.size:\(MemoryLayout.size(ofValue: person))")
print("person.stride:\(MemoryLayout.stride(ofValue: person))")
print("person.alignment:\(MemoryLayout.alignment(ofValue: person))")

打印结果
myPoint.size:17
myPoint.stride:24
myPoint.alignment:8
myClass.size:8
myClass.stride:8
myClass.alignment:8
student.size:8
student.stride:8
student.alignment:8
person.size:8
person.stride:8
person.alignment:8




根据上述代码我们可以清楚的看到类对象的size、stride、alignment都是8个字节,这是因为struct类型是值类型数据,class是对象类型数据,使用MemoryLayout对class类型计算其内存结果实际上是对其class类型的引用指针进行操作!

你可能感兴趣的:(oc基础)