This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Using `const int' values to size stack arrays


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



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]