• 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: Creating NSStrings without copying memory
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Creating NSStrings without copying memory


  • Subject: Re: Creating NSStrings without copying memory
  • From: Mike Abdullah <email@hidden>
  • Date: Tue, 2 May 2017 22:42:03 +0200

Drop down to CFString. It has similar api to the NSData one you like.

Mike.

Sent from my iPhone

> On 2 May 2017, at 22:11, Jens Alfke <email@hidden> wrote:
>
> I’ve got a heap block containing many UTF-8 strings, and I’m looking for the cheapest way to create NSString objects from them, i.e. without copying all the characters.
>
> -initWithBytesNoCopy:length:freeWhenDone: would be perfect, except that the string bytes are not an individual malloc block (so I can’t use freeWhenDone:YES), and I shouldn’t leave the big heap block in memory longer than necessary (so I can’t use freeWhenDone:NO.)
>
> NSData has -initWithBytesNoCopy:length:deallocator:, which is exactly what I want: the callback block tells me when the object doesn’t need the original bytes anymore, so I can reference-count that heap block and free it when there are no clients left. But NSString doesn’t have this :(
>
> If I created an NSData from the bytes this way, and then used that object to initialize an NSString, would I get what I want? Or would the NSString initializer just copy the bytes from the NSData without retaining it? Has anyone experimented with this?
>
> —Jens
>
> PS: I’m aware of the tradeoffs of this technique, i.e. that a client holding on to one small NSString could inadvertently keep the large heap block around.
> _______________________________________________
>
> 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
_______________________________________________

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: Creating NSStrings without copying memory
      • From: Jens Alfke <email@hidden>
References: 
 >Creating NSStrings without copying memory (From: Jens Alfke <email@hidden>)

  • Prev by Date: Creating NSStrings without copying memory
  • Next by Date: Re: Creating NSStrings without copying memory
  • Previous by thread: Creating NSStrings without copying memory
  • Next by thread: Re: Creating NSStrings without copying memory
  • Index(es):
    • Date
    • Thread