20200525--swift语法之UnsafePointer结构体(指针)

Generic StructureUnsafePointerA pointer for accessing data of a specific type. --用于访问特定类型数据的指针。Declaration@frozen struct UnsafePointer<Pointee>OverviewYou use instances of the UnsafePointer type to access data of a specific ty...
摘要由CSDN通过智能技术生成

Generic Structure

UnsafePointer

A pointer for accessing data of a specific type.

       --用于访问特定类型数据的指针
 

Declaration          --声明

@frozen struct UnsafePointer<Pointee>

Overview              --概览

You use instances of the UnsafePointer type to access data of a specific type in memory. The type of data that a pointer can access is the pointer’s Pointee type. UnsafePointer provides no automated memory management or 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.

         --您可以使用UnsafePointer类型的实例来访问内存中特定类型的数据。指针可以访问的数据类型是指针的指针类型。UnsafePointer不提供自动内存管理或对齐的保证您要负责通过不安全的指针处理任何内存的生命周期,以避免泄漏或未定义的行为。

Memory that you manually manage can be either untyped or bound to a specific type. You use the UnsafePointer type to access and manage memory that has been bound to a specific type.

        --您手动管理的内存可以是非类型化的,也可以绑定到特定的类型。您可以使用UnsafePointer类型来访问和管理绑定到特定类型的内存。

 

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

The memory referenced by an UnsafePointer 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.

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

 

Uninitialized Memory         --未初始化内存

Memory that has just been allocated through a typed pointer or has been deinitialized is in an uninitialized state. Uninitialized memory must be initialized before it can be accessed for reading.

           --刚刚通过类型化指针分配的内存或已取消初始化的内存处于未初始化状态。未初始化的内存必须先初始化,才能访问以供读取。

Initialized Memory                 --初始化内存

Initialized memory has a value that can be read using a pointer’s pointee property or through subscript notation. In the following example, ptr is a pointer to memory initialized with a value of 23:

              --初始化的内存有一个值,该值可以使用指针的 指向单元属性 或通过 下标符号 读取。在下面的例子中,ptr是一个指向初始化值为23的内存的指针

let ptr: UnsafePointer<Int> = ...
// ptr.pointee == 23
// ptr[0] == 23

 

Accessing a Pointer’s Memory as a Different Type             --变更指针类型来访问内存

When you access memory through an UnsafePointer instance, the Pointee type must be consistent with the bound type of the memory. If you do need to access memory that is bound to one type as a different type, Swift’s pointer types provide type-safe ways to temporarily or permanently change the bound type of the memory, or to load typed instances directly from raw memory.

          --当你通过一个UnsafePointer实例访问内存时,指针类型必须

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在C语言中,结构体指针是指向结构体对象的指针。通过使用结构体指针,可以对结构体进行动态操作和传递。例如,可以使用结构体指针来传递结构体作为函数参数,从而避免复制大量数据。结构体指针也可以用于动态分配内存,以便在程序运行时动态创建结构体对象。 使用结构体指针的方法有很多,其中一种常见的方法是将结构体指针作为函数参数传递,并在函数内部对结构体进行修改。这样可以避免复制整个结构体对象,提高了程序的效率。然而,需要注意的是,在使用结构体指针时,需要确保指针指向的内存空间是有效的,并要注意内存管理,避免内存泄漏或访问非法内存。 若想了解更多关于C语言结构体指针的使用方法,可以参考上述提供的参考资料。这些资料提供了关于C语言结构体指针的详细说明和示例代码,可以帮助您更好地理解和应用结构体指针。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [C语言中函数如何返回结构体?](https://blog.csdn.net/Dontla/article/details/118543022)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值