简体   繁体   中英

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 to 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM