malloc memory objective-c class

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.
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
 
Strange ... !

You did try the Class? I get the run time error during the allocation init call in my main.m file:

Code:
	DynamicVector *dynVec = [[DynamicVector alloc] initWithSize: elements];

The error is: main.m:19: error: incompatible type for argument 1 of 'initWithSize:'

Here is my main.m:
Code:
#import <stdio.h>
#import <stdlib.h>
@class DynamicVector;

int main(int argc, char *argv[])
{
	unsigned int elements;
	elements = 10;
	
	DynamicVector *dynVec = [[DynamicVector alloc] initWithSize: elements];
	
	[dynVec release];

      return 0;
	
}
 
Think I've solved it but don't know why it was an error!

Elements is the number of elements in the array.

Firstly, I put @class in the main rather than imported the header (teachs me for doing this kind of stuff at 2:30am)! Secondly I needed to change the method name -initWithSize to -initWithElements.... it said it had conflicting methods with that name? -initWithSize already appears in Cocoa I guess. Shouldn't the runtime know which one to call?

Here is the code that works for me:

main.m
Code:
#import <stdio.h>
#import <stdlib.h>
#import "DynamicVector.h"

int main(int argc, char *argv[])
{
	unsigned int elements;
	elements = 10;
	
	DynamicVector *dynVec = [[DynamicVector alloc] initWithElements: elements];
	
	[dynVec release];

    return 0;
	
}

Header
Code:
#import <Cocoa/Cocoa.h>

@interface DynamicVector : NSObject 
{
	double *array;
}

- (id) initWithElements: (unsigned int) n;
-(void) dealloc;

@end
Source
Code:
#import "DynamicVector.h"
#import <stdlib.h>

@implementation DynamicVector

- (id) initWithElements: (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
 
Back
Top