Re: Strange malloc/free issue inside a category
Re: Strange malloc/free issue inside a category
- Subject: Re: Strange malloc/free issue inside a category
- From: Shawn Erickson <email@hidden>
- Date: Sun, 30 Jul 2006 11:25:43 -0700
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