Mailing Lists: Apple Mailing Lists
Image of Mac OS face in stamp
Re: Strange malloc/free issue inside a category
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Strange malloc/free issue inside a category




On Jul 30, 2006, at 10:38 AM, Ken Tozier wrote:

@implementation NSData (Hex_Extensions)

- (NSString *) unicharHexStringFromData
{
int slen = [self length],
dLen = slen * sizeof(unichar);

unsigned char *ss = (unsigned char *) [self bytes],
*se = ss + slen,
ch;

unichar *dbuf = (unichar *) malloc (dLen), // if I comment this out
//dbuf[4096], // and use this instead, no crashes or errors
*ds = dbuf;

Just wanted to note that this implementation results in potentially a lot of memory allocation if your NSData instance is large. You are using a unichar which is a two byte character but are placing in that only characters that are assured to need single byte characters and since the hex rep of binary requires 2 characters per byte you are incurring an overhead of 4 times the size of the NSData instance. Of course depending on what your end target is you may not be able to avoid this overhead.


Personally I would use of unformatted base64 when you need to convert binary data to something that can fit in character storage (you can do better then base64 as well depending on the character limits of your storage).

-Shawn
_______________________________________________
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: 
 >Strange malloc/free issue inside a category (From: Ken Tozier <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2011 Apple Inc. All rights reserved.