malloc property objective c
Currently I am using NSMutableArray as a property. However I am also using opengl and for performance purposes I want to use malloc to create an a pointer to the int array and have this as the property.
How would I do this in objective c and still make sure that my memory is safe? Perhaps this is not even a safe thing t开发者_开发技巧o do in objective c? Mixing malloc with properties.
You can have pointers as properties. You're going to have to manage the memory yourself though, (ie since it won't be an objective c object, it can't be automatically retained and release.)
The following should work.
@interface ClassWithProperties : NSObject {
int *pointer;
}
@property int *pointer;
@end
@implementation ClassWithProperties
@synthesize pointer;
- (void) initializePointer {
self.pointer = malloc(sizeof(int) * 8);
}
- (void) dealloc {
free(self.pointer);
}
@end
精彩评论