How to write into an array from a dispatch_apply (GCD) loop?

天涯浪子 提交于 2019-11-29 10:45:18

error: cannot refer to declaration with an array type inside block

Under the blocks implementation, it is not allowed to access to an C array from blocks. (I can't find the documentation about it...)

There is an easy fix :-)

double valuesArray[SpaceSize], k1Array[SpaceSize];    
double *values = valuesArray, *k1 = k1Array;

To store into a captured variable, you need to add the __block storage specifier to its declaration. That should fix the std::vector problem you're seeing.

The issue with a variably-modified type makes it sound like you're using a variable-length array. These can't be referred to from within blocks, because they can't reliably be copied into the block's context (basically a custom C struct type), and they can mess up offsetof-style calculations when working with the context. You can solve this problem by working with a fixed-size variable (just use the maximum size you'll need), moving the arrays into global/static storage, or allocating a pointer and doing the pointer arithmetic to access the second dimension yourself.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!