|
#1
| |||
| |||
| malloc memory objective-c class 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; Header Code: #import <Cocoa/Cocoa.h>
@interface DynamicVector : NSObject
{
double *array;
}
- (id) initWithSize: (unsigned int) n;
-(void) dealloc;
@end 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 Last edited by boyfarrell; January 22nd, 2006 at 09:16 PM. |
|
#2
| |||
| |||
| What's the error you're getting? It compiled and ran fine for me. |
|
#3
| |||
| |||
| 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]; 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;
} |
|
#4
| |||
| |||
| What is "elements"? Post your code where you're calling it from. |
|
#5
| |||
| |||
| 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;
} Code: #import <Cocoa/Cocoa.h>
@interface DynamicVector : NSObject
{
double *array;
}
- (id) initWithElements: (unsigned int) n;
-(void) dealloc;
@end 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 Last edited by boyfarrell; January 22nd, 2006 at 09:37 PM. |
![]() |
| Thread Tools | |
|
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| objective-c class question | Gnomo | Software Programming & Web Scripting | 3 | February 22nd, 2004 11:34 PM |
| The built-in memory test has detected a problem with cache memory. Please contact... | antonioconte | Hardware & Peripherals | 1 | February 12th, 2003 09:55 AM |
| imac 266 Mhz - memory problem - shows double the memory | gagix | Hardware & Peripherals | 1 | September 16th, 2002 08:39 PM |
| Silly malloc.h problem | R1ck5P | Mac OS X System & Mac Software | 1 | January 14th, 2002 11:06 AM |
| Using First Class with OSX | jimmy love | Mac OS X System & Mac Software | 1 | December 15th, 2000 07:31 PM |