CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jun 2002
    Location
    Germany
    Posts
    1,557

    Initialization of const int within scope

    Hello,

    Is a fundamental data type with the const qualifier within a function scope initialized at run-time?

    I think that it is clearly specified for C++ and that there is, indeed, a run-time initialization of such a variable. However, is this also the case for plain ANSI C?

    See the example below written in old-fashioned pure ANSI C. Is n_rand initialized at run-time? Is this specified in the ANSI/ISO specification or is it a language extension? I can not find any mention of this language usage in the ANSI C specification.

    Thanks a lot for any help clarifying this issue.
    Chris.

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    static const int rand100(void)
    {
      // Return a random integer ranging from 0...99
      return (int) ((100.0 * (double) rand()) / RAND_MAX);
    }
    
    int main(int argc, char* argv[])
    {
      int i;
      for(i = 0; i < 100; i++)
      {
        // Is this a run-time initialization?
        const int n_rand = rand100();
    
        fprintf(stdout, "%i\n", n_rand);
      }
      return 1;
    }
    You're gonna go blind staring into that box all day.

  2. #2
    Join Date
    Oct 2002
    Location
    Tx, US
    Posts
    208
    I think const is recent addition to C std.
    & the C std says..

    The syntax and semantics of const were adapted from C++;

    HTH
    Vinod

  3. #3
    Join Date
    Nov 2002
    Location
    Foggy California
    Posts
    1,245
    In the code you wrote, n_rand is initialized at run time. The "const" just means that (a) you promise not to modify it's value (through some clever typecasting and assignment) and (b) the compiler can make some optimizations concerning n_rand.

    You will notice though that you declare n_rand within a for loop, which means that each time through the for loop it gets intantiated and thus initialized. Most compilers will probably allocate the memory at the beginning of main(), but you are forcing n_rand to be initialized 100 times.

    - Kevin

  4. #4
    Join Date
    Jun 2002
    Location
    Germany
    Posts
    1,557

    registered...

    Thanks for the comments.
    I agree.

    By the way, in the languages C++ or C does the combination of qualifiers "register const" make any sense? I would imagine yes if one wanted to hint to the compiler to put a non-modifiable POD-type within a CPU register.

    Sincerely, Chris.

    You're gonna go blind staring into that box all day.

  5. #5
    Join Date
    Jul 2002
    Location
    American Continent
    Posts
    340
    Code:
    static const int rand100(void)
    {
      // Return a random integer ranging from 0...99
      return (int) ((100.0 * (double) rand()) / RAND_MAX);
    }
    The const above does NOT mean that the function promises to always return a constant. What it means is that the returned integer is a "const int", i.e., you can't modify it.

    Such constness has no practical meaning whatsoever. Even if a function returns a regular, none-const integer. I don't see how you can modify the return value, after the function returns. There is no way to do that.

    The only thing you can do with an integer returned from a function is either to immediately make a copy into another variable, or immediately make a copy into a parameter used in another function call. In either usage the returned integer is copied and then right away you lose access to the original return value, and you only have a copy at hand.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured