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(unsignedint) 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.
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.:)
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.
@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?