Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 701
Zack117 Avatar asked Oct 26 '25 20:10

Zack117


1 Answers

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..

So: 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..

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]..


So far, uint8Pointer is just a shallow copy of uint8Pointer2.. Changing one affects the other..

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..

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..

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.

Hopefully I explained it correctly..

P.S. Name your variables properly!


Translation for the C++ people:

//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.
like image 72
Brandon Avatar answered Oct 28 '25 12:10

Brandon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!