On 12/17/2010 10:27 AM, pete wrote:
> Hello everybody --
>
> I always want the compiler to completely evaluate sizeof values. But I
> gather that C99 allows the run-time environment to do some partial
> evaluation of sizeof.
>
> If this is so, then is there some C99-compliant mechanism I can use to
> be sure that sizeof is evaluated always at compile time, never at run
> time?
No, because the size of a variable-length array (VLA) cannot be
determined until run time.
void func(int len) {
double vla[len]; // size unknown at compile time
size_t size = sizeof vla; // unknown at compile time
...
Aside from VLA's, though, everything that has a size at all has
a size that's an integer constant expression. There is no guarantee
that an ICE is "evaluated" at compile time, but they nearly always
are. Even if the "evaluation" occurs at run time, an ICE can be used
anywhere an plain integer constant can be.
--
Eric Sosman
lid