0

From Programming in C (3rd edition), page 156

In the case of const variables, they can be stored in read-only memory. So they may not be initialized each time the function is entered.

I Just wanna make sure, does that mean that const local variables are implicitly static?

3 Answers 3

1

In computer programming, a static variable is a variable that has been allocated statically—whose lifetime or "extent" extends across the entire run of the program.

Static variables are stored in the data segment of the program's address space (if initialized), or the BSS segment (if uninitialized).

A global or static variable can be declared (or a symbol defined in assembly) with a keyword qualifier such as const, constant, or final meaning that its value will be set at compile time and should not be changeable at runtime.

Compilers generally put static constants in the text section of an object file along with the code itself, as opposed to the data section where non-const initialized data is kept, though some have an option to produce a section specifically dedicated to constants, if so desired. Memory protection can be applied to this area to prevent overwriting of constant variables by errant pointers.

1

The foregoing answers overlook an important distinction between const and static const, which is that the former generates a set of mov instructions to initialize the variables at runtime, whereas the latter generates data stored at fixed addresses that gets baked into the object file.

For example, say we have the following array of strings.

static const char *         lpchrTestFiles [ ] =
{
   "..\\..\\Test_Files\\uchrMessage1.TXT"  ,
   "..\\..\\Test_Files\\uchrMessage2a.TXT" ,
   "..\\..\\Test_Files\\uchrMessage3.TXT"
} ; // static char * lpchrTestFiles [ ]

The string literals are always baked into the code. However, declaring the array of pointers as both static and const causes the compiler to generate an array of const pointers to the three strings, which are baked into the code.

If we eliminate the static keyword, leaving only const, the function prologue is immediately followed by three mov instructions that initialize a function scoped array of pointers, as shown in the following disassembly view taken from a Visual Studio debugging session.

   203:     const char *            lpchrTestFiles [ ] =
   204:     {
   205:         "..\\..\\Test_Files\\uchrMessage1.TXT"  ,
   206:         "..\\..\\Test_Files\\uchrMessage2a.TXT" ,
   207:         "..\\..\\Test_Files\\uchrMessage3.TXT"
   208:     } ; // static char * lpchrTestFiles [ ]
00EA1E00  mov         dword ptr [lpchrTestFiles],offset string "..\\..\\Test_Files\\uchrMessage1.TX"... (0EA5584h)  
00EA1E07  mov         dword ptr [ebp-1Ch],offset string "..\\..\\Test_Files\\uchrMessage2a.T"... (0EA55A8h)  
00EA1E0E  mov         dword ptr [ebp-18h],offset string "..\\..\\Test_Files\\uchrMessage3.TX"... (0EA55CCh)  

Unless you like wasting machine instructions or must minimize the size of the on-disk code image file, static const generates much more efficient code.

0

It turns out I was just a bit confused and the two things are totally different, separate things that have nothing in common. I was confused as I read the following lines

A static, local variable is initialized only once at the start of overall program execution—and not each time that the function is called.

which weirdly made me tie the two together in my head since you cannot initialize a const variable more than once. However, I think the book was not precise enough about static variables as you can actually have your static variable initialized more than once by having an assignment statement as follows

static int x;
x = 10; // re-initializes x to 10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .