Initialized and uninitialized global const variable in c

Forgive me, I am not very well versed in matters. So I start with an example directly

Take a look at the following example

const int a=10;
int *ptr;

int main(){
    ptr=&a; 
    *ptr=100;   // program crashes
    printf("%d",a);
}

      

But if I change the code above a bit by following

const int a; // uninitialized global variable 

      

Then the above code works fine.

So my question is, why does the compiler behave differently to uninitialize and initialize globals?

I am using gcc for windows (mingw).

+3


source to share


3 answers


You are modifying a const object and this is just undefined behavior - so don't do this and ignore compiler warnings.

Now, the actual reason for the different behavior in your particular case is that for the const int a=10;

value 10

must be stored somewhere. Since the variable is const, the linker places it in the .rodata or similar read-only section of the executable. When you try to write to a read-only location, you will get a segmentation fault.



For an uninitialized case, the const int a

value a

must be initialized to zero, since it is in the file scope (or; a

is a global variable). The linker then places the variable in the .bss section along with other data, which is also initialized to zero when the program starts. Bss section is read / written and you don't get a segfault when trying to write to it.

All of this is nothing to rely on, this may change with minor code modification if you are using a different compiler or a newer / older version of your compiler, etc.

+8


source


Global and static variables are implicitly initialized unless your code does it explicitly, as stipulated by the C standard.

From the doc:

const is a type classifier. Another type of classifier is volatile. The purpose of const is to declare objects that can be placed in read-only memory and potentially increase room for optimization.



In g ++, you get an error for the second case, i.e. const int a;

...

6.9.2 Defining external objects

Semantics

1 If an identifier declaration for an object has a file size and an initializer, the declaration is an external definition for Identifier.

2 Declaring an identifier for a file-scoped object with no initializer and no storage class specifier, or with a static storage class specifier, is a provisional definition. If the translation unit contains one or more tentative definitions for an identifier, and the translation unit does not contain an external definition for that identifier, then the behavior is exactly as if the translation unit contains an identifier scope declaration, with a composite type at the end of the translation unit with an initializer of 0.

+5


source


declares a constant integer variable. This means that its value cannot be changed. It is initially assigned a value of 10. If you try to change its value later, the compiler will issue a warning or error, depending on your compiler settings.

0


source







All Articles