Honey Honey - 1 month ago 6x
Objective-C Question

Why don't mutable objects require double pointers?

I read this answer http://stackoverflow.com/a/833124/5175709 and some other questions. From what I understand it's that since the object could expand and run out of space then the memory location my also change. I never understood why we shouldn't use

since they could also expand with any
ing or
ing and be in need of more space.

Please correct me where I understood wrong. I am confused.


Object references are only required to be single pointers as it does not relate to the memory they manage. For example the NSData class (the super class of NSMutableData) probably contains the following instance variables:

(this is conjecture as I don't have access to the source code)

@interface NSData : NSObject
    void *_data;
    NSUInteger _length;

If the NSMutableData subclass wants to add to the data buffer it can perform a realloc() on _data and increase _length.

The pointer to the NSMutableData object itself does not change at all and the memory it manages is hidden from the developer.