添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
So here is a piece of code. It's in C but I don't think that matters (does it?).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#define SZ_GENERATOR_STATE 624
unsigned int * InitializeGenerator(unsigned int Seed){
    static unsigned int *GeneratorState = (unsigned int *)
                              malloc(SZ_GENERATOR_STATE * sizeof(unsigned int));
    if (GeneratorState==NULL) return GeneratorState;
    else {
        GeneratorState[0] = Seed;
        int i; for (i=1; i<SZ_GENERATOR_STATE; i++) {
            GeneratorState[i] = 1812433253 * (GeneratorState[i-1] ^ 
                                                     GeneratorState[i-1] >> 30);
    return GeneratorState;
When compiled the compiler (gcc version 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5)) returns the following error:
InitGenr.c:14:31: error: initializer element is not constant
The error is in the line where I use malloc to allocate SZ_GENERATOR_STATE * sizeof ( unsigned int ) memory to an unsigned integer array... What's wrong with this?
I Googled for similar problems and almost all of them have a scope issue. But I'm not allocating memory in global scope. Or maybe I don't completely understand the problem. Please guide.
Many thanks. I think the compiler does not like that the variable declared as static. That is the compiler assumes that static variables shall be initialized by constant expressions.
In C++ this code is compiled successfully. So it is important whether C or C++ is used.:) Last edited on In C, static variables/objects must be initialised with a constant expression or an aggregate initialiser-list with constant expressions. Since function calls are not constant expressions, even when they yield a constant variable/object, they cannot be used to initialise a static variable/object. In fact, in C, variables/objects declared constant are not constant expressions; only magic constants are constant expressions. Therefore, the initialisation of GeneratorState is invalid.
Wazzak So what should I do?
If I remove the static keyword, I will not be able to return GeneratorState .
Should I not allocate memory dynamically? You can rewrite your function for example the following way
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#define SZ_GENERATOR_STATE 624
unsigned int * InitializeGenerator(unsigned int Seed)
    static unsigned int *GeneratorState = NULL;
    if ( !GeneratorState )
        GeneratorState = (unsigned int *)
                              malloc(SZ_GENERATOR_STATE * sizeof(unsigned int));
    if (GeneratorState==NULL) return GeneratorState;
    else {
        GeneratorState[0] = Seed;
        int i; for (i=1; i<SZ_GENERATOR_STATE; i++) {
            GeneratorState[i] = 1812433253 * (GeneratorState[i-1] ^ 
                                                     GeneratorState[i-1] >> 30);
    return GeneratorState;
Though I do not understand why do you have to declare the pointer as static.
Last edited on 
@vlad from moscow: Thank you very much for the alternate implementation.
You see, I'm trying to write a relatively simple implementation of Mersenne Twister (http://en.wikipedia.org/wiki/Mersenne_twister) in C. The array or its pointer, GeneratorState gets passed around between functions a lot. I don't know why, but I had a hunch that defining it as static might come in handy. Should I get rid of that?