This code all works, but I’m basically blundering around my lack of knowledge in C. The code works and does seem to be faster than creating NData instances every time I need to make a method call. But is it okay (no leaks, no pointer gotchas)?
I’m particularly worried about the cast to Byte* which was necessary to get the compiler to pipe down:
Here’s is the code, simplified:
- (BOOL) isThisMethodOkay {
// I have a length, range and an NSData instance
Byte bytes[self.data.length];
[self.data getBytes:&bytes range:range];
return [self doSomething:bytes length:length]
}
- (BOOL) whatAboutThis {
return [self doSomething:(Byte*)self.data.bytes length:self.data.length];
}
- (BOOL) doSomething:(Byte*)bytes length:(NSUInteger)length {
return (length == CHECK_LENGTH && data1(bytes) == CHECK_DATA_1);
}
static int data1(Byte* bytes) {
int retVal = (int)bytes[1];
return retVal;
}
Note: all code is under ARC.
There are no memory leaks since
bytesis a VLA which uses the stack. If you plan on handling any large amounts of data you may want to consider allocating that data on the heap and then freeing it when done. (You also should be able to useuint8_tinstead ofByte)