Search code examples
objective-ccmemoryconstantsstring-literals

Do string literals in different files have the same memory address?


I'm using objc_[sg]etAssociatedObject(), and this function uses a memory address as a key. If I pass the same string literal - e.g. "UIImageForTexture" - into the function from two different files, will the two string literals have:

  1. the same memory address?
  2. different memory addresses?
  3. it depends on some other factors.
  4. it's undefined.

I can do this more explicitly by sticking the literal in a file somewhere and referring to it via an extern but I'd like to know if I can avoid having to do that.


Solution

  • This is going to be an implementation defined behavior from the C99 draft standard section 6.4.5 String literals paragraph 6 says:

    It is unspecified whether these arrays are distinct provided their elements have the appropriate values. If the program attempts to modify such an array, the behavior is undefined.