how can I ensure that sizeof is evaluated at compile time rather thanat run time?

P

pete

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?

Thanks!

-- pete
 
E

Eric Sosman

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.
 
P

pete

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.

Ah, that's the answer! If I don't use VLAs, then sizeof should always
be known at compile time. Thanks, Eric.
 
S

Seebs

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?

Yes. Never call it on a VLA.

-s
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top