How does NSMutableString work?

I know that all instances of NSString are not processable. If you assign a new value to a row, then the new memory is addressed and the old row will be lost.

But if you use NSMutableString, the string will always keep the same address in memory, no matter what you do.

I wonder how it works. Using methods like replaceCharactersInRange, I can even add more characters to the string, so I need more memory for my string. What happens to objects in memory that follow a line? Are they all moved and placed somewhere else in memory? I do not think so. But what is going on?

+4
source share
2 answers

I know that all instances of NSString are inmutable. If you assign a new value to a new memory line and the old line will be lost.

This is not how volatility works, and how links to NSStrings work. How pointers do not work.

Pointer to an object - NSString *a; declares a variable a , which is a pointer to an object, simply stores the address in the memory of the object. The actual object is [usually] the allocation in the memory heap that contains the actual object itself.

In these terms, there is no run-time difference between:

 NSString *a; NSMutableString *b; 

Both are references to - addresses - some distribution in memory. The only difference is during compilation, b will be handled differently than a , and the compiler will not complain if, say, you use the NSMutableString methods when calling b (but when calling a ).

How NSMutableString works, it contains an internal buffer (or several buffers - implementation details) that contain string data. When you call one of the methods that mutate the contents of a string, the modified string will reassign its internal memory, if necessary, to contain new data.

Objects do not move in memory. After allocation, the distribution will never move - the object address or distribution will never change. The only exception is when you use something like realloc() , which may return a different address. However, this is really just a sequence of free(); malloc(); memcpy(); free(); malloc(); memcpy(); .

I suggest you return to the Objective-C Programming Guide, or perhaps the C Programming Guide .

+11
source

NSMutableString works just like C ++ std :: string do. I don’t know exactly how they work, but there are two popular approaches:

concating

you create a structure with two variables. one char and one pointer. when a new char (or more) is added, you create a new instance of the structure and add the address to the last instance of the row structure. this way you have a bunch of structures with a pointer directing the next structure.

copy and add

how will most beginners go. not the worst, but perhaps the slowest. you keep a "normal" immutable string. if a new char is added, you will allocate an area in memory with the size of the old line +1, copy the old line and strengthen the new char. this is a very simple approach, isn't it? the more difficult version is to create a new line with a size of +50 and just add characters and a new null at the end, do not forget to overwrite the old null . thus, it is more efficient for a string with a lot of changes.

as I said, I don’t know how std :: string or NSMutableString approaches this problem. but these are the most common ways.

0
source

Source: https://habr.com/ru/post/1314024/


All Articles