Can someone explain where and how string constants are stored by the compiler and how they're accessed by the runtime?
String Constant. A character string, a string constant consists of a sequence of characters enclosed in double quotes. A string constant may consist of any combination of digits, letters, escaped sequences and spaces. Note that a character constant ۥAۥ.
The string in Objective-C programming language is represented using NSString and its subclass NSMutableString provides several ways for creating string objects. The simplest way to create a string object is to use the Objective-C @"...". construct −. A simple example for creating and printing a string is shown below.
The string constant "A" consists of character A and \0. However, a single character string constant does not have an equivalent integer value. It occupies two. bytes, one for the ASCII code of A and another for the NULL character with a value 0, which is used to terminate all strings.
The maximum length of a character constant can be one character. A string constant can be any length. A single character string constant has an equivalent integer value. A single character string constant does not have an equivalent integer value. The character constant ‘A’ consists of only character A.
First the obligatory: you should shouldn't care how the compiler does this; anything based on how the compiler does this is a dangerous reliance on something that is not guaranteed and can change based on how the compiler optimizes. Do not write code based on this. Really. OK, we've got that out of the way.
Say you have code like this:
NSString *something = @"I'm a constant";
The compiler will generate this:
.section __TEXT,__cstring,cstring_literals
l_.str: ## @.str
.asciz "I'm a constant"
As you see, it's stored in the __TEXT section, along with your code, as a cstring literal. Over in __DATA, it'll store the constant CFString itself like this:
.section __DATA,__cfstring
.align 4 ## @_unnamed_cfstring_
L__unnamed_cfstring_:
.quad ___CFConstantStringClassReference
.long 1992 ## 0x7c8
.space 4
.quad l_.str
.quad 14 ## 0xe
First, it stores the CFType
(CFConstantStringClassReference
). Then internal information about the string (is it immutable, how is it deallocated, is it unicode, etc), a pointer to the cstring, and a length (14). If you want the details on the structure, pull down the CF sources from opensource.apple.com and look at CFString.c
. It explains the whole "internal information" field pretty well. (Pull them from Snow Leopard; Apple doesn't publish them as part of iOS, but they're the same.)
A second constant string would look like this, just to demonstrate how the symbol naming is done for the assembler.
.section __TEXT,__cstring,cstring_literals
l_.str2: ## @.str2
.asciz "%@"
.section __DATA,__cfstring
.align 4 ## @_unnamed_cfstring_3
L__unnamed_cfstring_3:
.quad ___CFConstantStringClassReference
.long 1992 ## 0x7c8
.space 4
.quad l_.str2
.quad 2 ## 0x2
If you want to get a better handle on this, just ask Xcode to generate the assembly for a simple file and see what it does. Oh, and of course you should never use this information because gcc
could change at any time. But it's great stuff to dig into.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With