• 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: Malloc and NSData
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Malloc and NSData


  • Subject: Re: Malloc and NSData
  • From: Clark Cox <email@hidden>
  • Date: Sun, 19 Dec 2004 21:40:01 -0500

On Sun, 19 Dec 2004 14:22:29 +0100, Pontus Ilbring
<email@hidden> wrote:
>
> On 2004-12-19, at 13.03, Peter Karlsson wrote:
>
> > Dear list!
> >
> > How do I copy the contents of a buffer (made with malloc) into a NSData
> > object?
>
> If you mean copy into an existing NSData object then the answer is that
> you don't. NSData is immutable.
>
> If you mean create a new NSData object with data from a buffer then try
> initWithBytes:length:

In addition, you can use dataWithBytesNoCopy... or initWithBytesNoCopy
to give the created NSData instance "ownership" of the malloced block:

size_t size = ...;
void *someBlock = malloc(size);
...
NSData *dataInstance = [NSData dataWithBytesNoCopy: someBlock length:
size freeWhenDone: YES];


Now, dataInstance "owns" the block of memory, and will free it
appropriately when dealloced.



--
Clark S. Cox III
email@hidden
http://www.livejournal.com/users/clarkcox3/
http://homepage.mac.com/clarkcox3/
 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

References: 
 >Malloc and NSData (From: "Peter Karlsson" <email@hidden>)
 >Re: Malloc and NSData (From: Pontus Ilbring <email@hidden>)

  • Prev by Date: Re: Disclosure Triangles
  • Next by Date: Re: Disclosure Triangles
  • Previous by thread: Re: Malloc and NSData
  • Next by thread: More experimenting
  • Index(es):
    • Date
    • Thread