Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Returning an Array of NSPoints from a Method

Returning an Array of NSPoints from a Method
Thread Tools
Senior User
Join Date: Oct 2000
Location: Lawrence, KS
Status: Offline
Reply With Quote
Aug 9, 2003, 12:50 PM
 
My C knowledge is somewhat limited. Below is method that's suppose to return an array of NSPoints. I can't get it to compile.

Code:
- (NSPoint ) nextPoints: (unsigned int) n { int i; NSPoint pointArray[n]; for( i=count; i < count+n; i++ ){ pointArray[i] = NSMakePoint( x[i], fx[i] ); } count = count + n; return pointArray; }

I tried a couple of other things, since NSPoint is just a C structure:

Code:
- (NSPoint [] ) nextPoints: (unsigned int) n - (NSPoint * ) nextPoints: (unsigned int) n
but so far no luck.

Besides the method signature, I could be making a boo boo when I define the the NSPoint array since I'm passing the dimension dynamically. C is is not supposed to like such things but Objective-C does. It gets a little confusing sometimes.

My first thought was to wrap the NSPoints in an object and the then stick them in an NSMutableArray. However, that seemed a little heavy handed.

Any tips appreciated.

Thanks,

     
Professional Poster
Join Date: Dec 2000
Location: Chicago, Illinois
Status: Offline
Reply With Quote
Aug 9, 2003, 01:24 PM
 
Unfortunately, you're going to have to use malloc to create a dynamic C array. It would look like this:

Code:
Point *array = (Point *)malloc(sizeof(Point)*n);
where n is the size of the array.

Another way to do it is to use NSMutableArray and fill it up with NSValue objects, which can hold most NS structs. The c way is going to be faster, of course.

Hope this helps,
Matt Fahrenbacher
(Last edited by Ghoser777; Aug 9, 2003 at 06:56 PM. )
     
DaGuy  (op)
Senior User
Join Date: Oct 2000
Location: Lawrence, KS
Status: Offline
Reply With Quote
Aug 9, 2003, 02:25 PM
 
Originally posted by Ghoser777:
Unfortunately, you're going to have to use malloc to create a dynamic C array. It would look like this:

[code]
Point *array = (Point *)malloc(sizeof(Point)*n);
[/code[

where n is the size of the array.

Another way to do it is to use NSMutableArray and fill it up with NSValue objects, which can hold most NS structs. The c way is going to be faster, of course.

Hope this helps,
Matt Fahrenbacher
Thanks, that brings in some perpective on the nature of the problem.

I will go with NSValue since I rather avoid managing the memory and I don't expect "n" to get very big. It will be somhere in 1 < n < 100.

I will keep the malloc suggestion in mind, for any future optimizations.

Thanks again!

     
Professional Poster
Join Date: Dec 2000
Location: Chicago, Illinois
Status: Offline
Reply With Quote
Aug 9, 2003, 06:59 PM
 
For the sake of full disclosure for anyone else who reads this thread and doesn't understand some of what we talkeda bout -

1) using malloc allocates dynamic memory, which means that when array in my example went out of scope, the memory was still allocated. To free up the memory, you would need to call free(array);

2) You could also just do this:

Code:
int array[100]
if n wouldn't exceed 100 and then you wouldn't have to worry about freeing the memory.

Matt Fahrenbacher
     
   
Thread Tools
Forum Links
Forum 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 On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 02:17 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2