image
image

Go Back   macosx.com > Design, Media, Programming & Scripting > Software Programming & Web Scripting

Reply
 
Thread Tools
  #1  
Old January 22nd, 2006, 07:59 PM
Registered User
 
Join Date: Jun 2005
Location: London, UK.
Posts: 278
Thanks: 0
Thanked 0 Times in 0 Posts
boyfarrell is on a distinguished road
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;
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

Last edited by boyfarrell; January 22nd, 2006 at 09:16 PM.
Reply With Quote
  #2  
Old January 22nd, 2006, 08:57 PM
Registered User
 
Join Date: Nov 2000
Posts: 948
Thanks: 0
Thanked 0 Times in 0 Posts
kainjow is on a distinguished road
What's the error you're getting? It compiled and ran fine for me.
Reply With Quote
  #3  
Old January 22nd, 2006, 09:14 PM
Registered User
 
Join Date: Jun 2005
Location: London, UK.
Posts: 278
Thanks: 0
Thanked 0 Times in 0 Posts
boyfarrell is on a distinguished road
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;
	
}
Reply With Quote
  #4  
Old January 22nd, 2006, 09:18 PM
Registered User
 
Join Date: Nov 2000
Posts: 948
Thanks: 0
Thanked 0 Times in 0 Posts
kainjow is on a distinguished road
What is "elements"? Post your code where you're calling it from.
Reply With Quote
  #5  
Old January 22nd, 2006, 09:22 PM
Registered User
 
Join Date: Jun 2005
Location: London, UK.
Posts: 278
Thanks: 0
Thanked 0 Times in 0 Posts
boyfarrell is on a distinguished road
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

Last edited by boyfarrell; January 22nd, 2006 at 09:37 PM.
Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off
Forum Jump

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


All times are GMT -5. The time now is 01:55 PM.


Mac Support® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0
Copyright 2000-2008 DigitalCrowd, Inc.