[英]Thread 1: Fatal error: UnsafeMutablePointer.initialize overlapping range
I am trying to use 我正在尝试使用
UnsafeMutablePointer<UnsafeMutablePointer<Float>?>!
as it is required for a parameter for a method i need to use. 因为它是我需要使用的方法的参数所必需的。 Yet I have no idea what this is or how to use it. 但是我不知道这是什么或如何使用。
I created this value by doing this : 我通过执行以下操作创建了此值:
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)
But I get the error : 但是我得到了错误:
Thread 1: Fatal error: UnsafeMutablePointer.initialize overlapping range
What am I doing wrong? 我究竟做错了什么?
You are creating bad behaviour.. 您正在制造不良行为。
var bytes2: [Float] = [39, 77, 111, 111, 102, 33, 39, 0]
let uint8Pointer2 = UnsafeMutablePointer<Float>.allocate(capacity: 8)
uint8Pointer2.initialize(from: &bytes2, count: 8)
Creates a pointer to some memory and initializes that memory to the values stored in bytes2
.. 创建一个指向某些内存的指针,并将该内存初始化为存储在bytes2
的值。
So: uint8Pointer2 = [39, 77, 111, 111, 102, 33, 39, 0]
因此: uint8Pointer2 = [39, 77, 111, 111, 102, 33, 39, 0]
Then you decided to create a pointer that references that pointer's memory: 然后,您决定创建一个引用该指针的内存的指针:
var uint8Pointer = UnsafeMutablePointer<Float>?.init(uint8Pointer2)
So if you printed uint8Pointer
, it would have the EXACT same values as uint8Pointer2
.. If you decided to change any of its values as well, it'd also change the values of uint8Pointer2
.. 因此,如果您打印uint8Pointer
,它将具有与uint8Pointer2
完全相同的值。如果您决定也更改其任何值,它也会更改uint8Pointer2
的值。
So when you do: 因此,当您这样做时:
var bytes: [Float] = [391, 771, 1111, 1111, 1012, 331, 319, 10]
uint8Pointer?.initialize(from: &bytes, count: 8)
It overwrote the values of uint8Pointer2
with [391, 771, 1111, 1111, 1012, 331, 319, 10]
.. 它用[391, 771, 1111, 1111, 1012, 331, 319, 10]
uint8Pointer2
覆盖了uint8Pointer2
的值。
So far, uint8Pointer
is just a shallow copy of uint8Pointer2
.. Changing one affects the other.. 到目前为止, uint8Pointer
是只是一个浅拷贝uint8Pointer2
..更改一个影响另一个..
Now you decided to do: 现在,您决定执行以下操作:
let uint8Pointer1 = UnsafeMutablePointer<UnsafeMutablePointer<Float>?>!.init(&uint8Pointer)
uint8Pointer1?.initialize(from: &uint8Pointer, count: 8)
Here you created a pointer ( uint8Pointer1
) to uint8Pointer
and you said uint8Pointer1
initialize with uint8Pointer
.. but you're initializing a pointer with a pointer to itself and a count of 8.. 在这里,您创建了一个指针( uint8Pointer1
)到uint8Pointer
和你说uint8Pointer1
初始化uint8Pointer
..但是你初始化的指针本身和8计数指针..
First of all, don't bother calling initialize on a pointer to pointer with a value of itself.. It's already pointing to the correct values.. 首先,不要打扰在指向具有自身值的指针的指针上调用initialize。它已经指向了正确的值。
What's nice is that: 很好的是:
uint8Pointer1?.initialize(from: &uint8Pointer, count: 1)
//Same as: memcpy(uint8Pointer1, &uint8Pointer, sizeof(uint8Pointer)`
//However, they both point to the same memory address..
will crash, but: 将崩溃,但是:
uint8Pointer1?.initialize(from: &uint8Pointer)
//Same as: `uint8Pointer1 = uint8Pointer`.. Note: Just a re-assignment.
won't.. because it doesn't do a memcpy
for the latter.. whereas the former does. 不会..因为它不会对后者memcpy
,而前者却不会。
Hopefully I explained it correctly.. 希望我能正确解释。
PS Name your variables properly! PS正确命名您的变量!
Translation for the C++ people: 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.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.