boyfarrell
Registered
Hi,
I'm trying to build a Objective-C wrapper for a malloc'ed C array using something like the below which runs prefectly.
My Objective-C Class compiles but gives an error at run time. Got any ideas how to write this as a Class? Here is what I have done so far:
Header
Source
I'm trying to build a Objective-C wrapper for a malloc'ed C array using something like the below which runs prefectly.
Code:
printf("Enter elements required: ");
unsigned int n;
scanf("%d", &n);
double *array;
array = (double *)calloc(n, sizeof(double));
if (array == NULL) {
printf("Cannot calloc memory for array\n");
exit(1); // End program, returning error status.
}
unsigned int i;
for(i=0; i < n; i++)
printf("\narray[%d] = %lf",i,array[i]);
free(array);
return 0;
My Objective-C Class compiles but gives an error at run time. Got any ideas how to write this as a Class? Here is what I have done so far:
Header
Code:
#import <Cocoa/Cocoa.h>
@interface DynamicVector : NSObject
{
double *array;
}
- (id) initWithSize: (unsigned int) n;
-(void) dealloc;
@end
Source
Code:
#import "DynamicVector.h"
#import <stdlib.h>
@implementation DynamicVector
- (id) initWithSize: (unsigned int) n
{
self = [super init];
if (self != nil)
{
array = (double *)calloc(n, sizeof(double));
if (array == NULL)
{
printf("Cannot calloc memory for array\n");
exit(1); // End program, returning error status.
}
}
return self;
}
-(void) dealloc
{
free(array);
[super dealloc];
}
@end