#1
  1. No Profile Picture
    .
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2002
    Posts
    296
    Rep Power
    12

    [c] sizeof: calculated at compile, or during run?


    in the k&r book it says :

    "c provides a compile-time unary operator called sizeof that can be used to compute the size of any object."

    but then in some notes on the net about this part of the book it says :

    "The sentence ``But the expression in the #define is not evaluated by the preprocessor'' means that, as far as the preprocessor is concerned, the ``value'' of the macro NKEYS (like the value of any macro) is just a string of characters..."

    the line of code in the k&r book is:

    Code:
    #define NKEYS (sizeof keytab / sizeof keytab[0])
    i thought basically #define replaced the text. nothing more. so in some code that has the above #define does NKEYS get replaced with this actual exact code:

    Code:
    (sizeof keytab / sizeof keytab[0])
    or does NKEYS get calculated, so therefore replaced with the value?

    this could be useful to answer this, but maybe not. keytab is defined like:
    Code:
    struct key {
    	char *word;
    	int count;
    } keytab[] = {
    	"auto", 0,
    	"break", 0,
    	"case", 0,
    	...
    	...
    	"volatile", 0,
    	"while", 0
    };
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,643
    Rep Power
    4248
    The process goes like this:
    Source Files --> Preprocessor --> Compiler --> Linker --> Executable file.

    The job of the preprocessor is to resolve all the #include and #define lines in the source code. What it does is, take the source file(s), perform search and replace operations, and produce an intermediate file for the compiler to compile.

    So, to answer your question, the preprocessor merely replaces all occurences of NKEYS with the string, "(sizeof keytab / sizeof keytab[0])". After the preprocessor is done, the intermediate file is passed on to the compiler.

    The process of actually determining sizeof keytab and sizeof keytab[0] is done by the compiler, which is the next stage in the process. If your compiler is smart enough (and also, depending on your optimization settings), it may even compute the result of sizeof keytab / sizeof keytab[0] at compile time, and replace the expression, with that computed value.
  4. #3
  5. No Profile Picture
    .
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2002
    Posts
    296
    Rep Power
    12
    i see. thanks.
    If your compiler is smart enough (and also, depending on your optimization settings), it may even compute the result of sizeof keytab / sizeof keytab[0] at compile time, and replace the expression, with that computed value.
    right, that's the part i was particularly interested in. so the #define part is irrelevent. it's a question of whether the compiler calculates, when it compiles, the size of keytab, and places that value in the end code, or places the calculation to get that value into the end code.

    what it's hopefully sizing up at compile time, keytab, is constant, so i'd hope, expect even, it is smart enough. how can i find out if the compiler is doing that, or make it do so? (i'm using apple's gcc 3.1 which is pretty similar to the usual gnu gcc, apart from it's got a few extra bits, i think)
  6. #4
  7. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,643
    Rep Power
    4248
    To make things clearer, let's say sizeof keytab array is 80 bytes and sizeof keytab[0] is 4 bytes. If your expression is something like this:
    foo = sizeof keytab / sizeof keytab[0];

    The compiler will know the sizeof keytab and sizeof keytab[0] at compile time. However, if the compiler is not smart enough, it may compile as though you typed this:
    foo = 80 / 4;
    If it is smart enough to optimize the expression, the code may end up as though you typed this:
    foo = 20;

    To determine what your compiler is doing, try compiling with the -S option. This will generate the intermediate assembly language file, so you can see how that statement was optimized. You might also consider tossing in different optimization option levels too (i.e. -O0, -O1, -O2 etc.) and see how it optimizes differently.
    Last edited by Scorpions4ever; May 21st, 2003 at 02:31 PM.
  8. #5
  9. No Profile Picture
    .
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2002
    Posts
    296
    Rep Power
    12
    i see. it's the calculation bit, not the sizeof bit that may or may not get done at compile time. sizeof gets done at compile time regardless of compiler smartness.

    thanks :)

IMN logo majestic logo threadwatch logo seochat tools logo