How do I return a C array from a method in Objective-C?

I have a function that returns a variable and I want to know how to return an array, the problem is it is not an NSArray, it is just an average C array like this ...

-(b2Fixture*) addFixturesToBody:(b2Body*)body forShapeName:(NSString*)shape
{
    BodyDef *so = [shapeObjects objectForKey:shape];
    assert(so);
       FixtureDef *fix = so->fixtures;
    int count = -1;
    b2Fixture *Fixi[4];
    while(fix)
    {

        count++;
        NSLog(@"count = %d",count);
        Fixi[count]= body->CreateFixture(&fix->fixture);
        if (Fixi[count]!=0) {
            NSLog(@"Fixi %d is not 0",count);
        }
        if (body->CreateFixture(&fix->fixture)!=0) {
            NSLog(@"body %d is not 0",count);
        }

        fix = fix->next;

    }

    return *Fixi;

}

      

If you see some types of variables that you are not aware of, because I am using the cocos2d framework to create a game, but I am returning the b2Fixture variable ... This code compiles, but only stores the value of the first block from the "fixi [0]" array not the whole array as i want to pass

anyhelp :) thankyou

+3


source to share


3 answers


You cannot return a local array. You will need to do some kind of dynamic allocation or pull a trick like having an array inside a struct.



Here's a link to an in-depth article that should help you.

+2


source


In general, returning C arrays by value is a bad idea, since arrays can be very large. Objective-C arrays are reference types - they are dynamically allocated, and a reference that is small is what is passed. You can dynamically allocate C arrays using one of the family malloc

for allocation and free

for deallocation.

You can pass C structs by value, and this is common since generic structs tend to be small (or small anyway).

Now in your case you are using a small array, it only has 4 elements. If you think that passing these 4 values ​​around the value makes sense and suits your design, then you can do it simply by inserting the C array into the C structure:



typedef struct
{
    b2Fixture *elements[4];
} b2FixtureArray;

...

-(b2FixtureArray) addFixturesToBody:(b2Body*)body forShapeName:(NSString*)shape
{
    BodyDef *so = [shapeObjects objectForKey:shape];
    assert(so);
    FixtureDef *fix = so->fixtures;
    int count = -1;
    b2FixtureArray Fixi;
    while(fix)
    {   
        count++;
        NSLog(@"count = %d", count);
        Fixi.elements[count]= body->CreateFixture(&fix->fixture);
        if (Fixi.elements[count] != 0)
        {
            NSLog(@"Fixi %d is not 0",count);
        }
        if (body->CreateFixture(&fix->fixture) != 0)
        {
            NSLog(@"body %d is not 0", count);
        }

        fix = fix->next;    
    }    
    return Fixi;    
}

...

// sample call outline
b2FixtureArray result = [self addFixturesToBody...]

      

Whether this standard "trick" of passing arrays by value is appropriate for your case, you have to decide.

Note. If it b2fixture

is an Objective-C object, make sure you understand the impact of memory management on C array object references depending on the memory management model you are using (MRC, ARC, GC).

+2


source


If you need to create a function or method that must return a fixed or bounded size array, one option is to pass a pointer to the result array to the function or method as a parameter. The caller can then take care of the space allocation or just use a local array of instances. You may want the called function to check that it works so that the array parameter is not NULL before using the array.

0


source







All Articles