Error[8]: Undefined offset: 2, File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 121
File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 473, decode(

概述在Swift 4中,MemoryLayout结构告诉您类型的大小,跨度和对齐方式. 我理解大小和步幅,但不是真正的对齐. 是否有一个示例显示了什么是对齐,它与步幅有什么不同,何时它与步幅有不同的值,以及使用步幅但使用对齐是否正确? 我可以一直计算另一个吗? 这是一个简单的例子: struct Foo { let a: Int16 let b: Int8}print(Memor 在Swift 4中,MemoryLayout结构告诉您类型的大小,跨度和对齐方式.

我理解大小和步幅,但不是真正的对齐.

是否有一个示例显示了什么是对齐,它与步幅有什么不同,何时它与步幅有不同的值,以及使用步幅但使用对齐是否正确?

我可以一直计算另一个吗?

这是一个简单的例子:
struct Foo {    let a: Int16    let b: Int8}print(MemoryLayout<Foo>.size)       // 3print(MemoryLayout<Foo>.alignment)  // 2print(MemoryLayout<Foo>.strIDe)     // 4

>结构的对齐是所有结构的最大对齐
字段,在这种情况下最大为2和1.
>结构的步幅是四舍五入到对齐,
这里3舍入到4的倍数.

步幅是内存中相同类型的连续实例(开始)之间的距离:

let array = [Foo(a: 1,b:2),Foo(a: 3,b: 4),Foo(a: 5,b: 6)]array.withUnsafeBytes {    print(Data([+++]) as NSData) // <01000234 03000474 0500066f>    print([+++].count) // 12}

struct strIDe是struct alignment的倍数,因此
所有实例(以及所有实例字段)都已正确对齐.

细节可以在
Type Layout:

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.
总结

以上是内存溢出为你收集整理的在Swift中对齐vs步幅全部内容,希望文章能够帮你解决在Swift中对齐vs步幅所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 166, include(/www/wwwroot/outofmemory.cn/tmp/route_read.php)
File: /www/wwwroot/outofmemory.cn/index.php, Line: 30, include(/www/wwwroot/outofmemory.cn/tmp/index.inc.php)
Error[8]: Undefined offset: 3, File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 121
File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 473, decode(

概述在Swift 4中,MemoryLayout结构告诉您类型的大小,跨度和对齐方式. 我理解大小和步幅,但不是真正的对齐. 是否有一个示例显示了什么是对齐,它与步幅有什么不同,何时它与步幅有不同的值,以及使用步幅但使用对齐是否正确? 我可以一直计算另一个吗? 这是一个简单的例子: struct Foo { let a: Int16 let b: Int8}print(Memor 在Swift 4中,MemoryLayout结构告诉您类型的大小,跨度和对齐方式.

我理解大小和步幅,但不是真正的对齐.

是否有一个示例显示了什么是对齐,它与步幅有什么不同,何时它与步幅有不同的值,以及使用步幅但使用对齐是否正确?

我可以一直计算另一个吗?

这是一个简单的例子:
struct Foo {    let a: Int16    let b: Int8}print(MemoryLayout<Foo>.size)       // 3print(MemoryLayout<Foo>.alignment)  // 2print(MemoryLayout<Foo>.strIDe)     // 4

>结构的对齐是所有结构的最大对齐
字段,在这种情况下最大为2和1.
>结构的步幅是四舍五入到对齐,
这里3舍入到4的倍数.

步幅是内存中相同类型的连续实例(开始)之间的距离:

let array = [Foo(a: 1,b:2),Foo(a: 3,b: 4),Foo(a: 5,b: 6)]array.withUnsafeBytes {    print(Data() as NSData) // <01000234 03000474 0500066f>    print([+++].count) // 12}

struct strIDe是struct alignment的倍数,因此
所有实例(以及所有实例字段)都已正确对齐.

细节可以在
Type Layout:

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.
总结

以上是内存溢出为你收集整理的在Swift中对齐vs步幅全部内容,希望文章能够帮你解决在Swift中对齐vs步幅所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 166, include(/www/wwwroot/outofmemory.cn/tmp/route_read.php)
File: /www/wwwroot/outofmemory.cn/index.php, Line: 30, include(/www/wwwroot/outofmemory.cn/tmp/index.inc.php)
在Swift中对齐vs步幅_app_内存溢出

在Swift中对齐vs步幅

在Swift中对齐vs步幅,第1张

概述在Swift 4中,MemoryLayout结构告诉您类型的大小,跨度和对齐方式. 我理解大小和步幅,但不是真正的对齐. 是否有一个示例显示了什么是对齐,它与步幅有什么不同,何时它与步幅有不同的值,以及使用步幅但使用对齐是否正确? 我可以一直计算另一个吗? 这是一个简单的例子: struct Foo { let a: Int16 let b: Int8}print(Memor 在Swift 4中,MemoryLayout结构告诉您类型的大小,跨度和对齐方式.

我理解大小和步幅,但不是真正的对齐.

是否有一个示例显示了什么是对齐,它与步幅有什么不同,何时它与步幅有不同的值,以及使用步幅但使用对齐是否正确?

我可以一直计算另一个吗?

这是一个简单的例子:
struct Foo {    let a: Int16    let b: Int8}print(MemoryLayout<Foo>.size)       // 3print(MemoryLayout<Foo>.alignment)  // 2print(MemoryLayout<Foo>.strIDe)     // 4

>结构的对齐是所有结构的最大对齐
字段,在这种情况下最大为2和1.
>结构的步幅是四舍五入到对齐,
这里3舍入到4的倍数.

步幅是内存中相同类型的连续实例(开始)之间的距离:

let array = [Foo(a: 1,b:2),Foo(a: 3,b: 4),Foo(a: 5,b: 6)]array.withUnsafeBytes {    print(Data() as NSData) // <01000234 03000474 0500066f>    print(.count) // 12}

struct strIDe是struct alignment的倍数,因此
所有实例(以及所有实例字段)都已正确对齐.

细节可以在
Type Layout:

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.
总结

以上是内存溢出为你收集整理的在Swift中对齐vs步幅全部内容,希望文章能够帮你解决在Swift中对齐vs步幅所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1048706.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存