我试图找到一种方法将未知固定大小的全局数组的所有元素初始化为非零值(0xffffffff)。
如果数组具有固定的已知大小,这不会有问题,因为我可以强制将每个元素的初始化强制为0xffffffff,如下所示:
static unsigned int array [4] = {0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff};不幸的是,我正在处理一个未知固定大小的数组。 在下面的示例中, SOME_CFG_OPTION是在构建时定义的。
#define ARRAY_SIZE ROUND_UP(SOME_CFG_OPTION); static unsigned int array [ARRAY_SIZE]; /* array in BSS and elements init'd to 0 */一个明显的答案是在构造函数或很早调用的函数中初始化数组 。 在我们的设置中,这不是一个选项,因为它对我们的启动时间有太大的影响。
当我们使用GNU或Intel C编译器构建时,以下方法对我们很有用。
#define ARRAY_SIZE ROUND_UP(SOME_CFG_OPTION); static unsigned int array [ARRAY_SIZE] = { [0 ... (ARRAY_SIZE- 1)] = 0xffffffff };但是,当我们使用DIAB C编译器(我们也必须支持)尝试此操作时,它会对该方法进行barfs,并出现以下错误
line #: error (etoa:4029): expected an expression = { [0 ... (ARRAY_SIZE - 1)] = 0xffffffff }; ^有关如何在使用DIAB C编译器进行编译时解决此问题的任何建议?
I am trying to find a way to initialize all the elements of a global array of unknown fixed size to a non-zero value (0xffffffff).
If array were of fixed known size, this would not be problem, as I could brute force the initialization of each element to 0xffffffff as with the following:
static unsigned int array [4] = {0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff};Unfortunately, I am dealing with an array of unknown fixed size. In the example below, SOME_CFG_OPTION is defined at build time.
#define ARRAY_SIZE ROUND_UP(SOME_CFG_OPTION); static unsigned int array [ARRAY_SIZE]; /* array in BSS and elements init'd to 0 */One obvious answer is to initialize array in a constructor or a function that is called very early. In our setup, this is not an option as it has too high an impact on our boot time.
The following worked well for us when we build with either the GNU or Intel C compilers.
#define ARRAY_SIZE ROUND_UP(SOME_CFG_OPTION); static unsigned int array [ARRAY_SIZE] = { [0 ... (ARRAY_SIZE- 1)] = 0xffffffff };However, when we try this with the DIAB C compiler (which we must also support), it barfs on that approach, with the following error
line #: error (etoa:4029): expected an expression = { [0 ... (ARRAY_SIZE - 1)] = 0xffffffff }; ^Any suggestions on how to solve this when compiling with the DIAB C Compiler?
最满意答案
Diab的语法分析器不能很好地解析“......”。 我大约一年前碰到了这个问题,我找到的唯一解决方案就是使用memset。
我这样做的方法是将所有内容设置为0.然后我定义了一个宏:
#define USE_MEM(x) ~x;我正在处理位操作,所以这很好。 我不会在bootcode中执行此操作,但反向具有很大影响。 在这种情况下,找出数据首次使用的位置并手动应用初始值可能更便宜,例如在首次使用该值之前添加mem = 0xffffffff。
我使用的是diab 5.2.1.0
Diab's syntax analyzer don't parse "..." very well. I ran into this about a year ago and the only solution I found was using memset.
The way I did it was to memset everything to 0. I then defined a macro:
#define USE_MEM(x) ~x;I was dealing with bit manipulation so this works fine. I would not do this in bootcode though as inverse have big impact. In case of that, it's probably less expensive to figure out where the data is first used and apply initial value manually, e.g. add mem = 0xffffffff right before the value first being used.
I was using diab 5.2.1.0
更多推荐
发布评论