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

Discussion in 'C Programming' started by pete, Dec 17, 2010.

  1. pete

    pete Guest

    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
    pete, Dec 17, 2010
    #1
    1. Advertising

  2. pete

    Eric Sosman Guest

    Re: how can I ensure that sizeof is evaluated at compile time ratherthan at run time?

    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
    Eric Sosman, Dec 17, 2010
    #2
    1. Advertising

  3. pete

    pete Guest

    Re: how can I ensure that sizeof is evaluated at compile time ratherthan at run time?

    On Dec 17, 10:46 am, Eric Sosman <> wrote:
    > 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
    >


    Ah, that's the answer! If I don't use VLAs, then sizeof should always
    be known at compile time. Thanks, Eric.
    --
    pete
    ----------
    patient: Doc, it hurts when I wiggle my VLAs.
    doctor: Then don't wiggle the damn things!
    pete, Dec 17, 2010
    #3
  4. pete

    Seebs Guest

    Re: how can I ensure that sizeof is evaluated at compile timerather than at run time?

    On 2010-12-17, pete <> wrote:
    > 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
    --
    Copyright 2010, all wrongs reversed. Peter Seebach /
    http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
    http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
    I am not speaking for my employer, although they do rent some of my opinions.
    Seebs, Dec 17, 2010
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Divick

    when is sizeof evaluated

    Divick, Oct 28, 2006, in forum: C++
    Replies:
    4
    Views:
    399
    Default User
    Oct 29, 2006
  2. Rick
    Replies:
    1
    Views:
    658
  3. Martin Ambuhl

    Re: Pointer dereference rather than sizeof?

    Martin Ambuhl, Aug 25, 2008, in forum: C Programming
    Replies:
    67
    Views:
    1,909
    Tim Rentsch
    Oct 9, 2008
  4. Keith Thompson

    Re: Pointer dereference rather than sizeof?

    Keith Thompson, Aug 25, 2008, in forum: C Programming
    Replies:
    8
    Views:
    610
    Bill Reid
    Sep 3, 2008
  5. CodeMonk3y
    Replies:
    28
    Views:
    6,576
    James Kanze
    Sep 18, 2008
Loading...

Share This Page