我正在尝试使用

UnsafeMutablePointer<UnsafeMutablePointer<Float>?>!

因为它是我需要使用的方法的参数所必需的。但是我不知道这是什么或如何使用。

我通过执行以下操作创建了此值:
 var bytes2: [Float] = [39, 77, 111, 111, 102, 33, 39, 0]
    let uint8Pointer2 = UnsafeMutablePointer<Float>.allocate(capacity: 8)
    uint8Pointer2.initialize(from: &bytes2, count: 8)

    var bytes: [Float] = [391, 771, 1111, 1111, 1012, 331, 319, 10]
    var uint8Pointer = UnsafeMutablePointer<Float>?.init(uint8Pointer2)
    uint8Pointer?.initialize(from: &bytes, count: 8)

    let uint8Pointer1 = UnsafeMutablePointer<UnsafeMutablePointer<Float>?>!.init(&uint8Pointer)
    uint8Pointer1?.initialize(from: &uint8Pointer, count: 8)

但是我得到了错误:
Thread 1: Fatal error: UnsafeMutablePointer.initialize overlapping range

我究竟做错了什么?

最佳答案

您正在制造不良行为。

var bytes2: [Float] = [39, 77, 111, 111, 102, 33, 39, 0]
let uint8Pointer2 = UnsafeMutablePointer<Float>.allocate(capacity: 8)
uint8Pointer2.initialize(from: &bytes2, count: 8)

创建指向某个内存的指针,并将该内存初始化为bytes2中存储的值。

因此:uint8Pointer2 = [39, 77, 111, 111, 102, 33, 39, 0]
然后,您决定创建一个引用该指针的内存的指针:
var uint8Pointer = UnsafeMutablePointer<Float>?.init(uint8Pointer2)
因此,如果您打印uint8Pointer,它将具有与uint8Pointer2完全相同的值。如果您决定也更改其任何值,它也会更改uint8Pointer2的值。

因此,当您这样做时:
var bytes: [Float] = [391, 771, 1111, 1111, 1012, 331, 319, 10]
uint8Pointer?.initialize(from: &bytes, count: 8)

它用uint8Pointer2 ..覆盖了[391, 771, 1111, 1111, 1012, 331, 319, 10]的值。

到目前为止,uint8Pointer只是uint8Pointer2的浅表副本。更改其中一个会影响另一个。

现在,您决定执行以下操作:
let uint8Pointer1 = UnsafeMutablePointer<UnsafeMutablePointer<Float>?>!.init(&uint8Pointer)
uint8Pointer1?.initialize(from: &uint8Pointer, count: 8)

在这里,您创建了一个指向uint8Pointer1的指针(uint8Pointer),并说uint8Pointer1使用uint8Pointer ..进行了初始化,但是您正在初始化一个指向其自身且具有8计数的指针。

首先,不要打扰在指向具有自身值的指针的指针上调用initialize。它已经指向了正确的值。

很好的是:
uint8Pointer1?.initialize(from: &uint8Pointer, count: 1)
//Same as:  memcpy(uint8Pointer1, &uint8Pointer, sizeof(uint8Pointer)`
//However, they both point to the same memory address..

将崩溃,但是:
uint8Pointer1?.initialize(from: &uint8Pointer)
//Same as: `uint8Pointer1 = uint8Pointer`.. Note: Just a re-assignment.

不会..因为它不为后者做memcpy ..而前者却做。

希望我能正确解释。

附言正确命名变量!

C++人士的翻译:
//Initial pointer to array..
float bytes2[] = {39, 77, 111, 111, 102, 33, 39, 0};
float* uint8Pointer2 = &bytes[2];
memcpy(uint8Pointer2, &bytes2[0], bytes2.size() * sizeof(float));

//Shallow/Shadowing Pointer...
float* uint8Pointer = uint8Pointer2;
float bytes[] = {391, 771, 1111, 1111, 1012, 331, 319, 10};
memcpy(uint8Pointer, &bytes[0], bytes.size() * sizeof(float));

//Pointer to pointer..
float** uint8Pointer1 = &uint8Pointer;

//Bad.. uint8Pointer1 and &uint8Pointer is the same damn thing (same memory address)..
//See the line above (float** uint8Pointer1 = &uint8Pointer)..
memcpy(uint8Pointer1, &uint8Pointer, 8 * sizeof(uint8Pointer));
//The memcpy is unnecessary because it already pointers to the same location.. plus it's also wrong lol.

关于ios - 线程1:致命错误:UnsafeMutablePointer.initialize重叠范围,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52175226/

10-09 02:21