-1
NSString *str = @"Hello World";

If operator overloading is not a thing in Objective-c how is this possible ? Is it built into a compiler ?

And one more question, how does it initialize it self ? what constructor is called ? does it allocate memory for a class it self or again some compiler magic ?

Anton Stafeyev
  • 761
  • 1
  • 7
  • 20
  • @RomanPodymov I am asking about assign operator, not @. In particular is this = operator built in to work with NSString to call initWithUTF8String method. – Anton Stafeyev Oct 18 '20 at 09:04
  • https://stackoverflow.com/a/433089/2229783 – Roman Podymov Oct 18 '20 at 09:06
  • @RomanPodymov but it doesn't say anything about allocation of an object, it creates a string via copy of contents, copy of pointer address, or anything related to releasing the mery used by that object – Anton Stafeyev Oct 18 '20 at 09:11
  • FWIW you tell the compiler via the ```-fconstant-string-class``` flag what to do with string constants. If you ever write your own framework this is required, e.g. if you use ObjFW you need to add ```-fconstant-string-class=OFConstantString -fno-constant-cfstrings``` to make it work. I think this may be your answer? – skaak Oct 18 '20 at 09:47

1 Answers1

1

Yes it's a builtin compiler mechanism for @"" string literals. There's no operator overloading involved here. The compiler assigns a __NSCFConstantString object that's already initialised when the Mach-O __DATA segment is processed during the program loading phase. You can verify this in this simple program:

NSString* globalString = @"globalString";
int main(int argc, const char * argv[]) {
    NSString* localString = @"localString";
    static NSString* staticString = @"staticString";
    NSString *heapString = [NSString stringWithFormat:@"heapStringExample"];
    return 0; //put breakpoint here
}

Using lldb:

(lldb) p localString
(__NSCFConstantString *) $1 = 0x0000000100001050 @"localString"
(lldb) p staticString
(__NSCFConstantString *) $2 = 0x0000000100001070 @"staticString"
(lldb) p globalString
(__NSCFConstantString *) $3 = 0x0000000100001030 @"globalString"
(lldb) p heapString
(__NSCFString *) $4 = 0x0000000103307f50 @"heapStringExample"
(lldb) memory region 0x0000000100001050
[0x0000000100001000-0x0000000100002000) rw- __DATA
(lldb) memory region 0x0000000103307f50
[0x0000000103300000-0x0000000103400000) rw-

You may notice localString, staticString and globalString all share memory range in the __DATA segment. That's opposed to heapString allocated on the heap.

This is a good article exploring the NSString class cluster internal implementations.

Kamil.S
  • 5,205
  • 2
  • 22
  • 51
  • Kamil thanks for your answer, I am coming from c background and some stuff is very new for me. So just to clarify, when I use NSString with const literals the whole object is initialized in data segment ? and there is no need to release any resources for it then. – Anton Stafeyev Oct 18 '20 at 13:55
  • @AntonStafeyev yes exactly – Kamil.S Oct 18 '20 at 14:35