Directly operating on memory pointed by UnsafeMutableRawPointer

In my project, i have a Swift class with a class level property of type string. Like this :

class TWSwiftString {
var pString:String!

init(_ pString: String) {
    self.pString = pString
}   
}

I am creating intance of this class and then creating a opaque pointer to this intance. Like this :

let str = TWSwiftString("World")
    
// Increasing RC by 1
strptr = Unmanaged.passRetained(str).toOpaque()
Now using this opaque pointer i want to modify the value of pString by directly operating on memory. Like this:

withUnsafeMutablePointer(to: &strptr.pString) { strPointer in
        strPointer.pointee = "World"
    }

Although i am able to modify pString like this and print. Lets assume i have a approach to make sure memory remains valid when it is operated on and freeing of memory is also handled somehow .

Will this approach work if i have 100s of intance of this string which are being operated in this manner ? What if the size of new value is greater than existing string value ? For this i am thinking of chunk of memory initially and then keep on increasing size of it as bigger string then this chunk comes. Does this approach seems feasible ? Any other problems i can encounter by using this approach ?

Chatgpt gave this answer :

To directly update the memory of a Swift class’s property, particularly to alter a String property, is generally discouraged due to Swift's memory safety model. However, if we want to access and modify a class property directly, the best practice is to use a property accessor, as manually altering memory could lead to undefined behavior or even crashes. Why Direct Memory Manipulation Is Risky When you attempt to manipulate memory directly, especially with Swift’s memory model, you might alter not only the value but also the memory layout of Swift’s String type, which could break things internally. The Swift compiler may store String differently based on the internal structure, so even if we manage to locate the correct memory address, directly modifying it is unreliable.

do you have any opinion around chatgpt resoponse ?

I’d like to clarify your goals here. You’re working with mutable pointers, which suggests that you want to modify the underlying bytes of the string. But the code that you posted doesn’t do that. The type of strPointer is UnsafeMutablePointer<String>, which means that strPointer.pointee is of type String, which tells you nothing about the memory in which the string is being stored.

You also tagged your thread with Inter-process communication, which further suggests that you’re doing some sort of serialisation for the purposes of IPC.

Finally, be aware that & doesn’t mean what you think it means if you’re coming from a C-based language. See The Peril of the Ampersand.

do you have any opinion around chatgpt resoponse ?

My experience is that LLMs generate very poor answers when it comes to subtle programming problems. This hasn’t done anything to change that opinion.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Directly operating on memory pointed by UnsafeMutableRawPointer
 
 
Q