Can someone explain where and how string constants are stored by the compiler and how they’re accessed by the runtime?
Can someone explain where and how string constants are stored by the compiler and
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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:
The compiler will generate this:
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:
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 atCFString.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.
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
gcccould change at any time. But it’s great stuff to dig into.