简体   繁体   中英

Calling C function from Objective-C loses passed in values

I'm trying to keep a float[] of my table row heights. I found a nice class for doing so here: http://forums.macnn.com/t/224809/nsmutablearray-vs-a-plain-c-array-for-storing-floats .

The problem is that once the C function (located in another file) gets called, the float that I passed in becomes 0. This happens every single time, regardless of the float value.

C function:

typedef struct
{
    float *array;
    int count;
} floatArray;

BOOL AddFloatToArray ( floatArray *farray, float newFloat )
{
    if ( farray->count > 0 )
    {
        // The array is already allocated, just enlarge it by one
        farray->array = realloc ( farray->array, ((farray->count + 1) * sizeof (float)) );

        // If there was an error, return NO
        if (farray->array == NULL)
            return NO;
    }
    else
    {
        // Allocate new array with the capacity for one float
        farray->array = (float *)malloc ( sizeof (float) );

        // If there was an error, return NO
        if (farray->array == NULL)
            return NO;
    }
    printf("Adding float to array %f\n", newFloat);
    farray->array[farray->count] = newFloat;
    farray->count += 1;
    printArrayContents(farray);
    return YES;
}

int printArrayContents( floatArray* farray)
{
    printf("Printing array contents\n");
    for(int j=0; j < farray->count; j++)
        printf("%f\n", farray->array[j]);


    return 0;
}

Called from:

    NSDictionary* review = [self.reviews objectAtIndex:indexPath.row];
    returnHeight = [ReviewCell cellHeightForReview:review];
    NSLog(@"Adding height to array: %0.0f", returnHeight);
    AddFloatToArray(heights, returnHeight);

Here's what gets logged:

2012-09-28 11:46:12.787 iOS-app[1605:c07] -[ProductDetailViewController tableView:heightForRowAtIndexPath:] [Line 598] Adding height to array: 101
Adding float to array 0.000000
Printing array contents
0.000000
2012-09-28 11:46:12.788 iOS-app[1605:c07] -[ProductDetailViewController tableView:heightForRowAtIndexPath:] [Line 598] Adding height to array: 138
Adding float to array 0.000000
Printing array contents
0.000000
0.000000
2012-09-28 11:46:12.788 iOS-app[1605:c07] -[ProductDetailViewController tableView:heightForRowAtIndexPath:] [Line 598] Adding height to array: 122
Adding float to array 0.000000
Printing array contents
0.000000
0.000000
0.000000
2012-09-28 11:46:12.789 iOS-app[1605:c07] -[ProductDetailViewController tableView:heightForRowAtIndexPath:] [Line 598] Adding height to array: 139
Adding float to array 0.000000
Printing array contents
0.000000
0.000000
0.000000

How can I ensure that the correct value is actually inserted into the float[]?

I didn't declare the functions in the .h I was #importing. So everything compiled and ran, but the classes weren't communicating correctly. You'd think that would raise a warning or error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM