200522--swift语法之UnsafeMutableRawPointer结构体

Structure

UnsafeMutableRawPointer

A raw pointer for accessing and manipulating untyped data.

        --用于访问和操作非类型化数据的原始指针。即对象类型的指针
 

Declaration

@frozen struct UnsafeMutableRawPointer

Overview       -- 概览

The UnsafeMutableRawPointer type provides no automated memory management, no type safety, and no alignment guarantees. You are responsible for handling the life cycle of any memory you work with through unsafe pointers, to avoid leaks or undefined behavior.

     --UnsafeMutableRawPointer类型不提供自动内存管理、类型安全性和对齐保证。您要负责处理 使用不安全指针时的 内存的生命周期,以避免内存泄漏或产生未定义的行为。

Memory that you manually manage can be either untyped or bound to a specific type. You use the UnsafeMutableRawPointer type to access and manage raw bytes in memory, whether or not that memory has been bound to a specific type.

       --您手动管理的内存可以是非类型化的,也可以绑定到特定的类型。您可以使用UnsafeMutableRawPointer类型来访问和管理内存中的原始字节,无论该内存是否已绑定到特定类型。

Understanding a Pointer’s Memory State       --理解指针的内存状态

The memory referenced by an UnsafeMutableRawPointer instance can be in one of several states. Many pointer operations must only be applied to pointers with memory in a specific state—you must keep track of the state of the memory you are working with and understand the changes to that state that different operations perform. Memory can be untyped and uninitialized, bound to a type and uninitialized, or bound to a type and initialized to a value. Finally, memory that was allocated previously may have been deallocated, leaving existing pointers referencing unallocated memory.

            --UnsafeMutableRawPointer实例引用的内存可以处于以下几种状态之一。许多指针操作 必须只限于 具有特定状态的内存 的指针,您必须跟踪正在处理的内存的状态,并了解不同操作对内存状态的修改。内存可以是未被类型化和未被初始化的,因此可以将内存绑定到类型但不进行初始化,也可以绑定到类型并初始化到值。最后,以前分配的内存可能已经被释放,但是却留下了 引用未分配内存的 指针

Raw, Uninitialized Memory       --原始的、未初始化的内存

Raw memory that has just been allocated is in an uninitialized, untyped state. Uninitialized memory must be initialized with values of a type before it can be used with any typed operations.

               --刚分配的原始内存处于未初始化、未类型化的状态。未初始化的内存必须使用类型值初始化,然后才能与任何类型化操作一起使用

You can use methods like initializeMemory(as:from:) and moveInitializeMemory(as:from:count:) to bind raw memory to a type and initialize it with a value or series of values. To bind uninitialized memory to a type without initializing it, use the bindMemory(to:count:) method. These methods all return typed pointers for further typed access to the memory.

           可以使用initializeMemory(as:from:)moveInitializeMemory(as:from:count:)方法将原始内存绑定到一个类型,并用一个或一系列值初始化它。要将未初始化的内存绑定到类型而不初始化它,请使用bindMemory(To:count:)方法。这些方法都返回类型化指针,以便对内存进行进一步的类型化访问。

Typed Memory        --类型化的内存

Memory that has been bound to a type, whether it is initialized or uninitialized, is typically accessed using typed pointers—instances of UnsafePointer and UnsafeMutablePointer. Initialization, assignment, and deinitialization can be performed using UnsafeMutablePointer methods.

       --被绑定到一个类型的内存,无论是初始化的还是未初始化的,通常使用UnsafePointer和UnsafeMutablePointer的类型化指针对象来访问。初始化、赋值和去初始化可以使用UnsafeMutablePointer方法来执行。

Memory that has been bound to a type can be rebound to a different type only after

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值