• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Is @"constantstring" pointer equal to @"constantstring" a guarantee?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Is @"constantstring" pointer equal to @"constantstring" a guarantee?


  • Subject: Re: Is @"constantstring" pointer equal to @"constantstring" a guarantee?
  • From: Jens Alfke <email@hidden>
  • Date: Thu, 27 Mar 2008 18:16:21 -0700


On 27 Mar '08, at 4:20 PM, Nathan Vander Wilt wrote:

So does that mean once I'm up at the Cocoa level, that
constant strings *are* guaranteed to have the same
pointer if their contents are the same? What is
"unique" and what is a "module" in this context?

The linker coalesces multiple identical string constants into a single value in the data segment. However, you can still end up with multiple copies if your code was linked in separate pieces and then joined together. Prior to Xcode 3.0 that used to happen when using ZeroLink — in fact, once or twice I've had my code crash when run with ZeroLink because I'd inadvertently used pointer comparison instead of isEqualToString: somewhere. Xcode 3.0 doesn't have ZeroLink anymore, but the details of how your program gets linked together are not something you should be relying on.


If you want to use this sort of optimization, it's very easy to write a MakeStringUnique( ) function that returns a single unique copy of the given NSString; any strings that have been run through that function can be compared with ==. All you have to do to implement it is keep a global NSMutableSet of NSStrings.

—Jens

Attachment: smime.p7s
Description: S/MIME cryptographic signature

_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: Is @"constantstring" pointer equal to @"constantstring" a guarantee?
      • From: "Kyle Sluder" <email@hidden>
References: 
 >Is @"constantstring" pointer equal to @"constantstring" a guarantee? (From: Nathan Vander Wilt <email@hidden>)

  • Prev by Date: Re: Subverting the first responder chain
  • Next by Date: Re: NSScroller question
  • Previous by thread: Re: Is @"constantstring" pointer equal to @"constantstring" a guarantee?
  • Next by thread: Re: Is @"constantstring" pointer equal to @"constantstring" a guarantee?
  • Index(es):
    • Date
    • Thread