This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Using `const int' values to size stack arrays
- From: Nathan Sidwell <nathan at codesourcery dot com>
- To: Daniel Towner <daniel dot towner at picochip dot com>
- Cc: gcc at gcc dot gnu dot org
- Date: Wed, 17 Nov 2004 10:46:30 +0000
- Subject: Re: Using `const int' values to size stack arrays
- Organization: Codesourcery LLC
- References: <419B27C3.3010009@picochip.com>
Daniel Towner wrote:
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?
I presume we're talking C here, and gnu C at that.
MAX_VAL is not an integral constant expression, so int ary[MAX_VAL]
must be a VLA, if it is valid.
C89 disallows both uses,
C99 disallows VLAs at file scope, but allows them at function scope.
GNU C is similar to C99.
In C++, MAX_VAL _is_ an integral constant expression.
nathan
--
Nathan Sidwell :: http://www.codesourcery.com :: CodeSourcery LLC
nathan@codesourcery.com :: http://www.planetfall.pwp.blueyonder.co.uk