Re: cannot access __block variable of array type inside block
Re: cannot access __block variable of array type inside block
- Subject: Re: cannot access __block variable of array type inside block
- From: Jonathon Kuo <email@hidden>
- Date: Tue, 14 Dec 2010 19:42:32 -0800
Thanks, Dave! That works, though I don't understand why...
On Dec 14, 2010, at 7:35 PM, Dave Zarzycki wrote:
> Jonathon,
>
> This is being tracked by our internal bug number 8590846. I'm told that the workaround is to place the array within a structure:
>
> __block struct {
> double result[COUNT];
> } results;
>
>
> davez
>
>
>
> On Dec 14, 2010, at 7:30 PM, Jonathon Kuo wrote:
>
>> I'm trying to learn GCD and reading http://developer.apple.com/library/mac/#featuredarticles/BlocksGCD/index.html
>> But the Global Concurrent Queues example in that document (last updated 2010-11-10) doesn't even compile. It complains about accessing array 'result' from within the block:
>>
>> #define COUNT 128
>> __block double result[COUNT];
>> dispatch_apply(COUNT, q_default, ^(size_t i){
>> result[i] = complex_calculation(i); /* <=== ERROR HERE!
>> });
>> double sum = 0;
>> for (int i=0; i < COUNT; i++) sum += result[i];
>>
>>
>> What is wrong here? This looks like it should work. Below is my full program.
>> -Jonathon
>>
>>
>> #include <dispatch/dispatch.h>
>> #include <stdio.h>
>> #define COUNT 128
>>
>> double complex_calculation( int i )
>> {
>> return i * 3.14;
>> }
>>
>> int main()
>> {
>> int i = 0;
>> __block double result[COUNT];
>>
>> dispatch_queue_t q_default;
>> /* get default queue */
>> q_default = dispatch_get_global_queue(0, 0);
>>
>> dispatch_apply(COUNT, q_default, ^(size_t i) {
>> result[i] = complex_calculation(i); /* <=== ERROR HERE!
>> });
>>
>> double sum = 0;
>> for (i=0; i < COUNT; i++) sum += result[i];
>> printf("%f\n", sum);
>> return 0;
>> }
>>
>> $ cc -g blk3.c -o blk3
>> blk3.c: In function ‘__main_block_invoke_1’:
>> blk3.c:20: error: cannot access __block variable of array type inside block
>>
>>
>> _______________________________________________
>>
>> Cocoa-dev mailing list (email@hidden)
>>
>> Please do not post admin requests or moderator comments to the list.
>> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>>
>> Help/Unsubscribe/Update your Subscription:
>>
>> This email sent to email@hidden
>
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden