Const Declaration

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • minalmk
    New Member
    • Jul 2007
    • 1

    Const Declaration

    When a const is declared in C where in Memory the value get stored?
  • Meetee
    Recognized Expert Contributor
    • Dec 2006
    • 928

    #2
    Originally posted by minalmk
    When a const is declared in C where in Memory the value get stored?
    The compiler is free to store constants wherever it wants (including non-writeable memory).

    Regards

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      The const value is stored based on the storage class specifier.

      [code=c]
      //In a function:
      auto const int var = 10; //stored in the stack frame
      const int var = 10; //same as auto const var = 10;
      static const int var = 10; //stored in the static memory pool
      register const int var = 10; //you suggest var to be stored in a processor register
      //
      //Outside a function:
      const int var = 10; //stored in the static memory pool with external linkage
      static const int var = 10; //stored in the static memory pool with internal linkage
      register const int var = 10; //you suggest var to be stored in a processor register
      [/code]

      Comment

      Working...