- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
谁能解释一下 UnsafeRawPointer.assumimgMemoryBound(to:)
和 UnsafeRawPointer.bindMemory(to:capacity:)
有什么区别?
一个编译或运行时间差异的实际例子会更受欢迎。
Swift Doc谈到 bindMemory(to:capacity:)
:
This API allows a region of memory to hold unrelated types at different points in the program. Binding uninitialized memory to a type prepares the memory to store values of that type. Binding memory that is already initialized reinterprets the in-memory values as the new type. If the old values are either nontrivial (require destruction) or if they are ever read from memory before being overwritten, then the new type must be mutually layout compatible with the old type.
这是什么意思将未初始化的内存绑定(bind)到一个类型会准备内存来存储该类型的值?它是分配的字节,对吧?那么 bindMemory(to:capacity:)
完成后有什么不同呢?
最佳答案
在 Swift 中分配的内存可以是:
当您使用 UnsafeMutableRawPointer.allocate(bytes:alignedTo:)
分配内存时, 你会得到未初始化的原始内存。
当您使用 UnsafeMutablePointer<T>.allocate(capacity:)
分配内存时, 你会得到绑定(bind)到类型 T
的未初始化内存.
bindMemory(to:capacity:)
(重新)将指针的内存绑定(bind)到新类型,并返回一个类型化的指针来访问它。在上述任何一种状态下,都可以在指向内存的指针上调用它;虽然如果内存被初始化,新的绑定(bind)类型必须与旧的绑定(bind)类型布局兼容,并且两种类型都应该是 trivial .
请注意,此方法不执行分配或初始化;它只是改变了内存的绑定(bind)类型。
assumingMemoryBound(to:)
是一种从原始指针获取类型化指针的方法,您已经知道指向绑定(bind)到给定类型的内存。如果内存没有绑定(bind)这个类型,那么通过返回的类型化指针访问内存是未定义的行为。
这里要注意的重要一点是,内存在给定时间只能绑定(bind)到一种类型。您可以自由地重新绑定(bind)到其他类型(具有上述初始化内存的限制);然而,试图以 unrelated 类型访问绑定(bind)到给定类型的内存违反了严格的别名,因此是未定义的行为。
另外需要注意的是,相关类型和布局兼容类型是独立的概念:
类型T
布局与 U
类型兼容如果内存绑定(bind)到类型 U
可以按位重新解释为具有类型 T
.请注意,这不一定是双向关系。例如,Int
布局是否与 (Int, Int)
兼容如果 (Int, Int)
的一个“实例”|可以重新解释为 2 x Int
.反过来不可能是真的。你不能形成 (Int, Int)
来自单个 Int
的值.
如果您可以将重叠的内存与这些类型重叠,那么这两种类型是相关的。例如,如果您有一个 UnsafePointer<T>
和 UnsafePointer<U>
, 如果 T
和 U
是不相关的类型,那么它们不能指向相互重叠的内存。
但是,我认为 Swift 尚未正式为这些术语定义任何规则(我预计这将伴随 ABI 稳定性)。
So what's different after
bindMemory(to:capacity:)
completes?
目前,什么都没有。如Andrew Trick says在 Martin linked to 的邮件列表讨论中:
Binding memory communicates to the compiler that the memory locations are safe for typed access. Nothing happens at runtime--until someone writes a type safety sanitizer. It affects the abstract state of the memory location, independent of the pointer variable used to access that memory. Binding memory returns a typed pointer for convenience and clarity, but there’s nothing special about that particular pointer value.
有关此主题的进一步阅读,请参阅 memory model explanation section of SE-0107 ,还有这个unofficial guide to strict aliasing in Swift .
关于swift - UnsafeRawPointer 假设MemoryBound 与 bindMemory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47940167/
我只想调用C函数 static inline uint64_t wyhash(const void* key, uint64_t len, uint64_t seed) 像这样来自 Swi
谁能解释一下 UnsafeRawPointer.assumimgMemoryBound(to:) 和 UnsafeRawPointer.bindMemory(to:capacity:) 有什么区别?
我想设置n Swift 中一个对象的关联对象数量 我的理解是UnsafeRawPointer的通常模式引用是这样的... static var reference = "someRef" public
下面的代码可以在 Swift Playground 中运行: import UIKit func aaa(_ key: UnsafeRawPointer!, _ value: Any! = nil)
我正在尝试执行 https 请求,但是当我尝试匹配本地和远程 ssl 证书时,Xcode 向我显示了上述错误。我在下面附上了一张错误图片和代码,如果有任何帮助,我们将不胜感激。 下面是URLSessi
我正在尝试获取设备 token 。 首先,这个值是独一无二的吗? 我承认它是一种独特的值(value),并努力获得它。我正在按照获取设备 token 的方式看到错误。 func applica
我是 UnsafeRawPointer 的新手。正如我所得到的,我需要在我的扩展中分配不安全的内存,并将指针发送到我的应用程序,在那里必须强制转换和释放它。下面是我想要做的草图。我想从 Safari
我正在研究带位置的相机。 我正在做的是当单击捕获图像按钮时,我将所有位置信息收集在字典中并进一步将其设置在 CFDictionarySetValue 中。这是我面临的问题。 CFDictionaryS
我在使用 MDQuery 浏览 Swift 3 中的 Spotlight 搜索结果时遇到了问题。我希望 MDQueryGetResultAtIndex 产生一个 MDItem,在 C/Objectiv
这是我在以前版本的 Swift 中的可行代码: let imageOptionsDictKeys = [ kCVPixelBufferPixelFormatTypeKey, kCVPixelBuff
根据 Swift 3 中 Data 的文档,我可以使用一个初始化程序从 UnsafeRawPointer 创建 Data。我需要的实际上是相反的。我有一个数据,我想创建一个指向数据字节的 Unsafe
IOHIDDeviceSetReport 方法不接受 UnsafeRawPointer 类型方法签名是: IOHIDDeviceSetReport(, , , ##UnsafePointer#>, )
我正在将一个 swift 2.3 项目更新到 3.1,我在转换这个函数时遇到了问题,特别是在一行中。 这是数据的扩展。 public func read(offset f_offset: inout
我有一个 UITableView。当点击一个单元格时,我的应用程序会生成相关图像。图像生成是在后台线程上完成的,因为一些图像需要相当长的时间来渲染。渲染图像后,将在主线程上调用 UIImageWrit
我正在尝试传递 Data? 中包含的字节到 C 函数。 C 函数声明如下: void func(const void *buffer); 我的 Swift 看起来像: myData?.withUnsa
我们有一个跨平台图像处理库,有一个调用将 CGImageRef 作为 UnsafeRawPointer 返回,我需要将其转换为 CGImage。我知道调用在 objective-c 中使用它时的工作方
无法将 Void 类型的值(又名“()”)转换为预期的参数类型“unsafeRawPointer” 代码允许我通过触摸在屏幕上画线。我正在使用字典来允许多个用户同时绘制多条线。 就在我试图将指针存储在
我想从字符串中生成 CRC32 哈希,所以我得到了 zlib 函数 crc32。 但在 Swift 3 中,这会产生问题。 代码如下所示: func crc32HashString(_ string:
我是一名优秀的程序员,十分优秀!