 |
 |
Returning an Array of NSPoints from a Method
|
 |
|
 |
|
Senior User
Join Date: Oct 2000
Location: Lawrence, KS
Status:
Offline
|
|
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
|
|
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.
)
|
|
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Oct 2000
Location: Lawrence, KS
Status:
Offline
|
|
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
|
|
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:
if n wouldn't exceed 100 and then you wouldn't have to worry about freeing the memory.
Matt Fahrenbacher
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|