cannot allocate an array of constant size 0

phungus420

Deity
Joined
Mar 1, 2003
Messages
6,296
How do I create this array?

int iReligionInfluence[GC.getNumReligionInfos()]

Compiler doesn't like it because:
"expected constant expression" (how is getNumReligionInfos() not constant? This cannot change mid game :dunno:)
"cannot allocate an array of constant size 0"

Anyone know any work arounds?
 
The following appears hundreds of times in the current sdk code:

m_piFlavorValue = new int[GC.getNumFlavorTypes()];
 
"Cannot change mid-game" is not the same thing as "constant" as far as the compiler is concerned. Constant means a constant C++ expression (5, 2 + 3, 3 << 1 + 5 * 218 / PI, etc), nothing involving a value read from disk or memory. For that, use "new" which is a function that accepts the size parameter.

Why does the compiler even care? Because the compiler will create the code to allocate the memory on the stack or heap itself among the other variables, and for that it must know the exact size it requires at compile time. When you use new, you are storing a memory address in the variable which is always 4 or 8 bytes (32- vs. 64-bit OS) that the compiler can allocate itself.
 
Back
Top Bottom