I would like to store (pointers to) dynamic arrays as the instance variables in objects, and be able to initialize the arrays to custom size. Like in this simple code:
@interface DummyClass: NSObject {
float * X;
}
@property float * X;
@end
@implementation DummyClass
@synthesize X;
-(id) init {
[super init];
X = malloc(100*sizeof(float));
}
@end
int main(int argc, const char * argv) {
float * mypointer;
DummyClass * myclass = [[DummyClass alloc] init];
mypointer = myclass.X;
mypointer[0] = 1;
NSLog(@"Vallue assigned succesfully");
getchar();
return 0;
}
This gives “Segmentation fault” error when trying to assign value to mypointer[0]. What’s the proper way of storing and accessing dynamic arrays within objects?
Your
initmethod needs to return something:Your program runs fine for me here after making that change (and after correcting the signature of
main). You should have had a warning or error about that mistake, though. How did you build & test your example program?