![](/img/trans.png)
[英]What does `UnsafeMutablePointer.initialize()`actually do?
[英]Thread 1: Fatal error: UnsafeMutablePointer.initialize overlapping range
我正在嘗試使用
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)
它用[391, 771, 1111, 1111, 1012, 331, 319, 10]
uint8Pointer2
覆蓋了uint8Pointer2
的值。
到目前為止, 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
,而前者卻不會。
希望我能正確解釋。
PS正確命名您的變量!
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.