Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > how can I ensure that sizeof is evaluated at compile time rather thanat run time?

Reply
Thread Tools

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

 
 
pete
Guest
Posts: n/a
 
      12-17-2010
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
 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      12-17-2010
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
http://www.velocityreviews.com/forums/(E-Mail Removed)lid
 
Reply With Quote
 
 
 
 
pete
Guest
Posts: n/a
 
      12-17-2010
On Dec 17, 10:46*am, Eric Sosman <(E-Mail Removed)> 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
> (E-Mail Removed)


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!
 
Reply With Quote
 
Seebs
Guest
Posts: n/a
 
      12-17-2010
On 2010-12-17, pete <(E-Mail Removed)> 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 / (E-Mail Removed)
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
problem in running a basic code in python 3.3.0 that includes HTML file Satabdi Mukherjee Python 1 04-04-2013 07:48 PM
sizeof() calculated at compile time or run time CodeMonk3y C++ 28 09-18-2008 07:57 AM
Compile warning Access of shared member will not be evaluated Rick ASP .Net 1 06-26-2007 08:16 PM
when is sizeof evaluated Divick C++ 4 10-29-2006 07:46 PM



Advertisments