开发者

malloc property objective c

开发者 https://www.devze.com 2022-12-08 03:31 出处:网络
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

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
0

精彩评论

暂无评论...
验证码 换一张
取 消