内存布局

struct S1 {
    var a: Int32
    var b: Int8
    var c: Int8
}

struct S2 {
    var a: Int8
    var b: Int32
    var c: Int8
}

struct S3 {
    var a: Int8
    var b: Int8
    var c: Int32
}


let size1 = MemoryLayout<S1>.size
let alignment1 = MemoryLayout<S1>.alignment
let stride1 = MemoryLayout<S1>.stride

let size2 = MemoryLayout<S2>.size
let alignment2 = MemoryLayout<S2>.alignment
let stride2 = MemoryLayout<S2>.stride

let size3 = MemoryLayout<S3>.size
let alignment3 = MemoryLayout<S3>.alignment
let stride3 = MemoryLayout<S3>.stride

// size1: 6, alignment1: 4, stride1: 8
// size2: 9, alignment2: 4, stride2: 12
// size3: 8, alignment3: 4, stride3: 8
#![allow(unused)]
fn main() {
struct S1 {
    a: i32,
    b: i8,
    c: i8,
}

struct S2 {
    a: i8,
    b: i32,
    c: i8,
}

struct S3 {
    a: i8,
    b: i8,
    c: i32,
}


use std::mem::{align_of, size_of};
println!("{} {}", size_of::<S1>(), align_of::<S1>());
println!("{} {}", size_of::<S2>(), align_of::<S2>());
println!("{} {}", size_of::<S3>(), align_of::<S3>());
}
struct S1 {
    int a;
    char b;
    char c;
};

struct S2 {
    char a;
    int b;
    char c;
};

struct S3 {
    char a;
    char b;
    int c;
};


unsigned long size1 = sizeof(struct S1);
unsigned long align1 = _Alignof(struct S1);
unsigned long size2 = sizeof(struct S2);
unsigned long align2 = _Alignof(struct S2);
unsigned long size3 = sizeof(struct S3);
unsigned long align3 = _Alignof(struct S3);

// size1: 8, align1: 4
// size2: 12, align2: 4
// size3: 8, align3: 4