Using `const int' values to size stack arrays

Daniel Towner daniel.towner@picochip.com
Wed Nov 17 10:47:00 GMT 2004


Hi all,

If I have a variable which I declare in one of the following ways:

  const int MAX_VAL = 12;

or

  static const int MAX_VAL = 12;

and I subsequently use it in expressions, gcc will substitute the value 
`12' wherever MAX_VAL appears, but not in the following case:

void someFn()
{
 int someArray[MAX_VAL];

  // etc...
}

In this case, `someArray' will be treated as a variable sized array, 
with subsequent performance impact. Why can't gcc treat it as an array 
which has 12 elements? Is this a gcc limitation, or a fundamental 
limitation of C?

thanks,

dan.

============================================================================
Daniel Towner
picoChip Designs Ltd., Riverside Buildings, 108, Walcot Street, BATH,
BA1 5BG
daniel.towner@picochip.com
07786 702589 




More information about the Gcc mailing list