views:

84

answers:

2

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

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

I wonder how this exactly works. With methods like replaceCharactersInRange I can even add more characters to a string so I need more memory for my string. What happens to the objects in memory that follow the string? Are they all relocated and put somewhere else in memory? I don't think so. But what is really going on?

+9  A: 

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

That isn't how mutability works, nor how references to NSStrings work. Nor how pointers work.

A pointer to an object -- NSString *a; declares a variable a that is a pointer to an object -- merely holds the address in memory of the object. The actual object is [generally] an allocation on the heap of memory that contains the actual object itself.

In those terms, there is really no difference at runtime between:

NSString *a;
NSMutableString *b;

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

As far as how NSMutableString works, it contains a buffer (or several buffers -- implementation detail) internally that contain the string data. When you call one of the methods that mutate the string's contents, the mutable string will re-allocate its internal storage as necessary to contain the new data.

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

I'd suggest you revisit the Objective-C Programming Guide or, possibly, a C programming manual.

bbum
A: 

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

concating

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

copy & add

the way most newbies will go. not the worst, but maybe the slowest. you save a "normal" unmutable string. if a new char is added, you allocate a area in the memory with the size of the old string +1, copy the old string and concate the new char. that's a very simple approach, isn't it? a bit more advanced version would be to create the new string with a size +50, and just add the chars and a new null at the end, don't forget the to overwrite the old null. this way it's more efficient for string with a lot of changes.

as i said before, i don't know how std::string or NSMutableString approaches this issue. but these are the most common ways.

M3t0r
NSMutableString and C++'s std::string are not really at all alike beyond both being string containers.
bbum