Suppose I’m getting a C string from some function:
char * mystring = SomeCFunction(...);
And I own this string (I’m responsible for freeing it when I’m done).
If, in Objective-C, I create an NSString * using:
NSString * mynsstring = [NSString stringWithCString:mystring
encoding:NSUTF8StringEncoding];
Am I still responsible for freeing the original C string?
I’m assuming the answer is yes, but I can’t find a definitive answer in the documentation – and the functionality of the opposite method (cStringUsingEncoding), while sensical, does give me pause, because it handles the cString freeing for you.
If the answer is yes, then am I also responsible for making sure I don’t free the c string before I’m done using the NSString *, or does the function copy the string for me? I ask this because the documentation for stringWithCString says it:
Returns a string containing the bytes in a given C array, interpreted
according to a given encoding.
Which still leaves me wondering if it actually copied the bytes, or is just pointing at them internally (and I’m basically just doing a cast).
It does copy the bytes.
If you don’t want the bytes to be copied, you may use
- (id)initWithBytesNoCopy:(void *)bytes length:(NSUInteger)length encoding:(NSStringEncoding)encoding freeWhenDone:(BOOL)flaginstead.If you use
- (id)initWithBytes:(const void *)bytes length:(NSUInteger)length encoding:(NSStringEncoding)encoding, or any commodity method likestringWithCString:encoding:that itself calls this low-level method, the bytes are copied.