tags:

views:

55

answers:

5
#include <stdio.h>

const int str[1000] = {0};

int main(void)
{
    printf("arr is %d\n", str[0]);
    return 0;
}

Has the following output:

[-exercises/adam/stack2]:size a.out
   text    data     bss     dec     hex filename
   5133     272      24    5429    1535 a.out

Whereas:

#include <stdio.h>

static int str[1000] = {0};

int main(void)
{
    printf("arr is %d\n", str[0]);
    return 0;
}

Has the following output:

[-exercises/adam/stack2]:size a.out
   text    data     bss     dec     hex filename
   1080    4292      24    5396    1514 a.out

When the array is uninitialized -- it again goes to text segment for "const" and to BSS for "static".

The variable is global and should be accessible from anywhere in the executable it is part of (because of no "static"), but given its a variable I don't know why it is placed in text segment instead of data segment?

+2  A: 

To allow memory protection to work. Any attempt to write to a const will trigger a segfault.

Oli Charlesworth
+3  A: 

You're confused. There is no dichotomy between const and static; the two are independent. Assuming all data is initialized, both static const and external (global) const will go in text and both non-const-qualified static and non-const-qualified external will go in data.

As for bss, modern binary formats like ELF actually have separate bss for constant and nonconstant zero data. The output of the size command just doesn't show it.

R..
You are right. Thanks for the clarification.
helpmelearn
+1  A: 

When you declare a variable const you're telling the compiler that you never intend to change its value. On the other hand, with the static declaration made at the file scope, you're telling the compiler that that variable is private to the compilation unit it has been declared in, but functions within that compilation unit are still allowed to modify this variable.

As Oli mentions in his answer, locating the const variable in the text segment allows the system to enforce memory access protection. Also, consider an embedded system, in that case the text segment is usually written to flash and is thus non-modifiable. The data, bss segments etc. are located in RAM and their modification is allowed.

Praetorian
+2  A: 

From Kernighan & Ritchie:

static is a storage class specifier. Other storage class specifiers are: auto, register, extern & typedef. The static specifier gives the declared objects static storage class. The static declaration, applied to an external variable or function, limits the scope of that object to the rest of the source file being compiled. Static objects may be local to a block or external to all blocks, but in either case retain their values across exit from and reentry to functions and blocks.

Whereas,

const is a type qualifier. The other type qualifier is volatile. The purpose of const is to announce objects that may be placed in read-only memory, and perhaps to increase opportunities for optimization.

I suppose one can infer that both these keywords serve distinct purposes; that of const variables being in text/code segment is quite clear from its purpose.

Kedar Soparkar
R..
A: 

By putting the const data into the text section, the compiler is trying to enforce constness.

Keep in mind that the TEXT section is loaded into memory pages marked read only in the MMU page tables. This is to guard against accidental corruption of code. By putting const data in the same area, makes that data read only as well. Any writes to this data will then invoke exceptions.

Uninitialized data declared static will go into BSS segment to conserve space in the executable file. This area is allocated in memory by the loader. Initialized data declared static will go into the DATA segment, which is read-write.

Ziffusion