views:

199

answers:

4

I found a c function which I would like to use in my app. Unfortunately, my c knowledge is not great. The first section of code shows the original c code and the second my "translation" to objective c. I have 3 questions I would appreciate help with please:

  1. Is my translation of the variables from their c counterparts to their objective c counterparts valid? (I have had no compiler warnings)
  2. Is it acceptable to use the free () at the end or should this be done in another way in objective c

c code:

unsigned int i, j, diagonal, cost, s1len, s2len;
unsigned int *arr;

char *str1, *str2;

general code...

s1len = strlen(str1);
s2len = strlen(str2);

arr = (unsigned int *) malloc(sizeof(unsigned int) * j);

general code...

free(arr);

objective c code:

NSUInteger i, j, diagonal, cost, s1len, s2len;
NSUInteger *arr;

const char *str1 = [source cStringUsingEncoding:NSISOLatin1StringEncoding];
const char *str2 = [target cStringUsingEncoding:NSISOLatin1StringEncoding];

general code...

s1len = strlen(str1);
s2len = strlen(str2);

arr = (NSUInteger *) malloc(sizeof(NSUInteger) * j);

general code...

free(arr);
+3  A: 

You can mix Objective C with pure C when developing for the iPhone. In general, with Objectvie C you want to be working with higher level objects and as such you shouldn't need to invoke malloc and similar (although of course you can).

I would suggest that you either re-implement the functionality the C code provides from scratch in Objective C, that is think about what you require the code to do and then just write the Objective C code - rather than trying to change the C code line by line. Or, I would just include the pure C code in your project and call the functions you need from Objective C.

pheelicks
+1  A: 

What's stopping you from just using the c function in your code as is? You can use any c function in Objective-c and it won't cause a problem. Many of the functions in Cocoa are c functions (for example NSSearchPathForDirectoriesInDomains().

nevan
NSSearchPathForDirectoriesInDomains is not C (Carbon). It returns a NSArray *, which does not exist outside Cocoa.
diciu
I thought NSArray was part of the Foundation Framework, which exists independently of Cocoa.
alesplin
+5  A: 

Objective-C is a strict superset of C, therefore you can use any C code without any modifications. I suggest either using the C code as is (as far as possible) or re-implementing the algorithm with objects in Objective-C.

NSString to char

What you need to provide, is a way to make convert Objective-C objects into C types, like NSString* to char*.

The conversion is correct, but you might want to use -UTF8String to keep all chars intact, Latin-1 might lose some information. The disadvantage of utf-8 is, that you're C code might not be able to correctly work with it.

You'd better get the lengths using one of NSString's methods instead of strlen, because it has linear running time and NSString's methods could be constant.

// utf-8
int len = [source length];
// latin 1
int len = [source lengthOfBytesUsingEncoding:NSISOLatin1StringEncoding];

int to NSInteger

There's no reason to convert ints to NSIntegers. Apple has added this type for 64bit compatibility. NSInteger is typedef'd so that on 32bit platforms it needs 32bit and on 64bit platforms 64bit.

I'd try to change as little as possible of the C code. (Makes it easier to update it when the original gets updated.) So just leave the ints as they are.

Memory management

C's memory management is more basic than Objective-C's, but as you seem to know you use malloc and free, that just stays the same. Retain/release and the garbage collector are only useful for objects anyway.

Georg
Hi, thanks for your great answer. The reason why I used the latin encoding is because of the diacritic insensitivity of utf8 which is of some importance for this function.
Run Loop
+1  A: 

Your C version is perfectly valid Objective-C code. You don't need to translate it.

mouviciel