# malloc memory objective-c class



## boyfarrell (Jan 22, 2006)

Hi,

I'm trying to build a Objective-C wrapper for a malloc'ed C array using something like the below which runs prefectly.

```
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*

```
#import <Cocoa/Cocoa.h>

@interface DynamicVector : NSObject 
{
	double *array;
}

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

@end
```

*Source*

```
#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
```


----------



## kainjow (Jan 22, 2006)

What's the error you're getting? It compiled and ran fine for me.


----------



## boyfarrell (Jan 22, 2006)

Strange ... !

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


```
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:

```
#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;
	
}
```


----------



## kainjow (Jan 22, 2006)

What is "elements"? Post your code where you're calling it from.


----------



## boyfarrell (Jan 22, 2006)

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*

```
#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*

```
#import <Cocoa/Cocoa.h>

@interface DynamicVector : NSObject 
{
	double *array;
}

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

@end
```
*Source*

```
#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
```


----------

