gpt4 book ai didi

swift - Swift 是否保证字段在类和结构中的存储顺序?

转载 作者:搜寻专家 更新时间:2023-10-30 21:49:23 25 4
gpt4 key购买 nike

在 C 中,您在结构中定义字段的顺序就是它们在内存中实例化的顺序。考虑到内存对齐,如图所示,以下结构在内存中的大小为 8 字节,但如果字段反转则只有 6 字节,因为不需要任何对齐填充。

struct s {
int32_t a;
/* 2 bytes of padding to align a 64 bit integer */
int64_t b;
}

这种顺序保证存在于 C 结构、C++ 类(和结构)和 Objective-C 类中。

对于 Swift 类和结构中的字段,存储顺序是否有类似的保证?或者(鉴于该语言不像其他列出的语言那样支持指针),编译器是否在编译时以最佳方式为您重新安排它们?

最佳答案

是的,struct元素在内存中的顺序就是他们的声明。详情可见在 Type Layout(强调)。但是请注意“当前”的使用,所以这个在 Swift 的 future 版本中可能会发生变化:

Fragile Struct and Tuple Layout

Structs and tuples currently share the same layout algorithm, noted as the "Universal" layout algorithm in the compiler implementation. The algorithm is as follows:

  • Start with a size of 0 and an alignment of 1.
  • Iterate through the fields, in element order for tuples, or in var declaration order for structs. For each field:
    • Update size by rounding up to the alignment of the field, that is, increasing it to the least value greater or equal to size and evenly divisible by the alignment of the field.
    • Assign the offset of the field to the current value of size.
    • Update size by adding the size of the field.
    • Update alignment to the max of alignment and the alignment of the field.
  • The final size and alignment are the size and alignment of the aggregate. The stride of the type is the final size rounded up to alignment.

填充/对齐不同于 C:

Note that this differs from C or LLVM's normal layout rules in that size and stride are distinct; whereas C layout requires that an embedded struct's size be padded out to its alignment and that nothing be laid out there, Swift layout allows an outer struct to lay out fields in the inner struct's tail padding, alignment permitting.

只有当一个结构是从 C 中导入时,它才保证有相同的内存布局。 Apple 的 Joe Groff 写道 [swift-users] Mapping C semantics to Swift

If you depend on a specific layout, you should define the struct in C and import it into Swift for now.

later in that discussion :

You can leave the struct defined in C and import it into Swift. Swift will respect C's layout.

例子:

struct A {
var a: UInt8 = 0
var b: UInt32 = 0
var c: UInt8 = 0
}

struct B {
var sa: A
var d: UInt8 = 0
}

// Swift 2:
print(sizeof(A), strideof(A)) // 9, 12
print(sizeof(B), strideof(B)) // 10, 12

// Swift 3:
print(MemoryLayout<A>.size, MemoryLayout<A>.stride) // 9, 12
print(MemoryLayout<B>.size, MemoryLayout<B>.stride) // 10, 12

此处 var d: UInt8 位于 var sa: A 的尾部填充中。如果您在 C 中定义相同的结构

struct  CA {
uint8_t a;
uint32_t b;
uint8_t c;
};

struct CB {
struct CA ca;
uint8_t d;
};

然后将其导入到 Swift 中

// Swift 2:
print(sizeof(CA), strideof(CA)) // 9, 12
print(sizeof(CB), strideof(CB)) // 13, 16

// Swift 3:
print(MemoryLayout<CA>.size, MemoryLayout<CA>.stride) // 12, 12
print(MemoryLayout<CB>.size, MemoryLayout<CB>.stride) // 16, 16

因为 uint8_t d 布局在 struct CA sa 的尾部填充之后。

从 Swift 3 开始,sizestride 返回相同的值(包括结构填充)从 C 导入的结构,即返回与 C 中的 sizeof 相同的值。

这是一个简单的函数,有助于演示上述内容(Swift 3):

func showMemory<T>(_ ptr: UnsafePointer<T>) {
let data = Data(bytes: UnsafeRawPointer(ptr), count: MemoryLayout<T>.size)
print(data as NSData)
}

Swift 中定义的结构:

var a = A(a: 0xaa, b: 0xbbbbbbbb, c: 0xcc)
showMemory(&a) // <aa000000 bbbbbbbb cc>

var b = B(sa: a, d: 0xdd)
showMemory(&b) // <aa000000 bbbbbbbb ccdd>

从 C 导入的结构:

var ca = CA(a: 0xaa, b: 0xbbbbbbbb, c: 0xcc)
showMemory(&ca) // <aa000000 bbbbbbbb cc000000>

var cb = CB(ca: ca, d: 0xdd)
showMemory(&cb) // <aa000000 bbbbbbbb cc000000 dd000000>

关于swift - Swift 是否保证字段在类和结构中的存储顺序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39302834/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com