Quick constant question
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Quick constant question

  1. #1
    Join Date
    Jun 2002
    Location
    Latvia, Riga
    Posts
    35

    Quick constant question

    I have a very quick question. If I declare a constant the standard way, with the const keyword, does it go into the data segment or the code segment of the executable?

  2. #2
    Join Date
    Jun 2002
    Location
    Germany
    Posts
    1,557
    Depends on the compiler as well as the context and some other qualifiers.

    Code:
    #include <math.h>
    
    // Probably data
    static const int n = 3;
    const int m = 5;
    
    int main(int argc, char* argv[])
    {
      // Probably a local register.
      const double d = ::sin(0.1);
    
      return 0;
    }
    Sincerely, Chris.

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

  3. #3
    Join Date
    Jun 2002
    Location
    Latvia, Riga
    Posts
    35
    Many thanks.

  4. #4
    Join Date
    Nov 2002
    Location
    Los Angeles, California
    Posts
    3,863
    There is a keyword "register" which will cause a variable to be
    stored in a machine register if possible. Note: MS says the
    32-bit compiler does not honor user requests for register
    variables.

    If you want to force the constant to be in the code segment, then
    you can use template meteprogramming to achieve this.
    refer to this papar and the section on compile time functions
    http://osl.iu.edu/~tveldhui/papers/T.../meta-art.html
    Wakeup in the morning and kick the day in the teeth!! Or something like that.

    "i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."

  5. #5
    Join Date
    Jun 2002
    Location
    Germany
    Posts
    1,557
    You know,


    Both last nicht as well as this morning, as soon as souldog mentioned that interesting comment about template programming, my respect for this topic was reawakened.

    In fact, it is really a very rich topic. The samples below show some of the interesting cases which can arise. Take a look.

    Sincerely, Chris.

    Code:
    #include <math.h>
    
    // Probably data
    static const int n = 3;
    const int m = 5;
    
    // I do not know?
    static const double dc = ::sqrt(0.3);
    
    // Template metaprogram
    template <int N> struct factorial
    {
      enum
      {
        value = factorial<N-1>::value * N
      };
    };
    
    template <> struct factorial<0>
    {
      enum
      {
        value = 1
      };
    };
    
    // Use of the template metaprogram.
    static volatile int four_factorial = static_cast<int>(factorial<4>::value);
    
    int main(int argc, char* argv[])
    {
      // Probably a local register.
      const double d = ::sin(0.1);
    
      // Maybe a register, maybe not.
      // Maybe considered bad style?
      register const int rn = 12;
    
      volatile const int vc = 34;
    
      return 0;
    }
    You're gonna go blind staring into that box all day.

  6. #6
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773
    The only difference between the const with static and the one without is a scoping one, and that is something the compiler can sort out. For your integral ones it may well optimise and treat them like enums.

    Even for one like

    Code:
    const long double pi = 3.1415926535897932384626433832795;
    the compiler may well substitute a literal everywhere pi is used.

    We already know that using string literals will lead to undefined behaviour if you try modifying them.

    For scoping purposes it is probably best to use namespaces to avoid naming clashes (better than using static with your const).

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center