iosfreak
iosfreak

Reputation: 5238

Return float array in c++

I am using a very large float array in conjunction with CIColorCube CoreImage to create filters. Since I am making many filters, the data gets repeated over and over again, and takes up to 3 minutes to compile (which is really annoying). Here's what I have:

- (void)performFilter {
    NSData * cube_data = [NSData dataWithBytes:[self colorCubeData] length:32*sizeof(float)];
    CIFilter *filter = [CIFilter filterWithName:@"CIColorCube"];
    [filter setValue:outputImage forKey:kCIInputImageKey];
    [filter setValue:@16 forKey:@"inputCubeDimension"];
    [filter setValue:cube_data forKey:@"inputCubeData"];
}

- (const void*)colorCubeData {
    float color_cube_data[32] = { 1,1,1,1,1,1,1,1.0 };
    return color_cube_data;
}

I scaled the code down A LOT. I get this error:

Address of stack memory associated with local variable 'color_cube_data' returned

I'm relatively new to c++, please help! It's probably a pretty stupid fix.

EDIT 1

Here's a snippet my actual code. Since I have multiple instances of CIColorCube that require the same format, I send each rgba channel to a selector and have it return a float array.

- (const void*)colorCubeData:(float)alpha redArray:(NSArray*)redArray blueArray:(NSArray*)blueArray greenArray:(NSArray*)greenArray {
    float r1 = [[redArray objectAtIndex:0] floatValue]/255.0f;
    float r2 = [[redArray objectAtIndex:1] floatValue]/255.0f;
    float b1 = [[blueArray objectAtIndex:0] floatValue]/255.0f;
    float b2 = [[blueArray objectAtIndex:1] floatValue]/255.0f;
    float g1 = [[greenArray objectAtIndex:0] floatValue]/255.0f;
    float g2 = [[greenArray objectAtIndex:1] floatValue]/255.0f;
    color_cube_data[16384] = { r1,g1,b1,1.0,r2,g1,b1,1.0 } 
}

Upvotes: 0

Views: 619

Answers (1)

Jorge Israel Peña
Jorge Israel Peña

Reputation: 38616

The problem is as the error says. You're returning the address to that array, but that array is scope-limited to that function, meaning that once the function is done, the address will no longer be safe to use (i.e. undefined behavior). You should declare float color_cube_data[32] at a higher scope (e.g. global, class, etc.) or dynamically allocate the array.

Upvotes: 1

Related Questions