Re: NSString array to -> char**
Re: NSString array to -> char**
- Subject: Re: NSString array to -> char**
- From: Chris Ridd <email@hidden>
- Date: Sun, 16 Mar 2003 07:36:08 +0000
On 16/3/03 5:02 am, Seth Delackner <email@hidden> wrote:
>
On Saturday, March 15, 2003, at 08:48 PM, Randy Zauhar wrote:
>
>
> Seth, you malloc a block of characters and set cargs[i] equal to the
>
> pointer to the block, and then immediately overwrite cargs[i] with the
>
> pointer returned by [arg cString]. Did you mean to do a strcpy?
>
>
Now that I think of it, I malloc some space, then instead of using that
>
space, use [arg cString]'s space, which is a big leak.
>
>
Could I just do
>
cargs[i] = (char*)[arg cString], and never bother to free cargs[i],
>
since [arg cString] is producing a (const char*)? I could later on
>
just free cargs, which is malloc'd as an array of pointers.
Nope, each element of the array is still malloc()ed, so you still need to
free() each one as well as free()ing the array.
There's also no need to collect all the NSStrings up in an NSMutableArray
since all you're actually trying to do here is find out how many of them
there are.
You can go through the variable arguments again by calling va_start(varargs,
arg1) right after calling va_end(varargs) the first time. Then your first
loop can just increment a counter.
>
>> cargs[i] = (char*)malloc(sizeof(char) * [arg length] + 1);
>
>> cargs[i] = (char*)[arg cString];
You should not use -cString, because it relies on a "default" encoding which
is not under your control, and conversion might fail. You should use
something like -dataUsingEncoding:allowLossyConversion: and then NSData's
-getBytes:. Try an encoding of NSUTF8StringEncoding, though you will really
need to check the thing you're calling to see what it is expecting.
Cheers,
Chris
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.