开发者

Creating a C Array from 2D NSArray

I have a 2D NSArray of string numbers that I would like to convert to a 2D C array of doubles for use with BLAS/LAPACK functions (through the accelerate framework). This line of code seems to work, however seems to be incredibly inefficient and eventually crashes due to a malloc error. Is there a more efficient way to convert this 2D NSArray to a C array? Or a convienent way of using NSArrays with BLAS/LAPACK?

double gridDataC[[nrows intValue]+1][[ncol intValue]+1];

for(i=6;i<[fileLines count]-1;i++){
    for(j=0;j<[ncol intValue]-1;j++){
        gridDataC[i][j]=[[[[fileLines objectAtIndex:i] componentsSeparatedByString:@" "] o开发者_开发技巧bjectAtIndex:j] doubleValue];
    }  
} 

fileLines is an array that contains lines of a file that are parsed into respective numbers.


There are few things here that deal with memory.

1.componentsSeparatedByString: creates an autoreleased array. Since you're looping for every object within that string, you are creating similar array multiple times. As the autoreleased objects are not released until the end of the runloop this might clog the memory. It's better to do this once by bringing the method call out of the inner loop.

2.The value of i is the most confusing. You pass i as the index for gridDataC. It should probably be i - 6 if you're starting from i = 6.

double gridDataC[[nrows intValue] + 1][[ncol intValue] + 1];

for( i = 6; i < [fileLines count] - 1; i++ ){
    NSArray * components = [[fileLines objectAtIndex:i] componentsSeparatedByString:@" "];
    for( j = 0; j < [ncol intValue] - 1; j++ ){
        gridDataC[i - 6][j] = [[components objectAtIndex:j] doubleValue];
    }  
} 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜