4

Pardon me, I am not very good in explaining questions. So I start with example directly

Look at following example

const int a=10;
int *ptr;

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

But If I made a slightly change in above code as following

const int a; // uninitialized global variable 

Then the above code works fine.

So my question is why compiler behaves differently for uninitialize and initialize global const variables?

I am using gcc for windows (mingw).

A.s. Bhullar
  • 2,680
  • 2
  • 26
  • 32

3 Answers3

13

You are modifying a const object, and that is simply undefined behavior - so don't do it, and don't ignore compiler warnings.

Now, the actual reason for the different behavior in your particular case is that for const int a=10; the value 10 has to be stored somewhere. Since the variable is const, the linker places it in the .rodata or a similar read only section of the executable. When you're trying to write to a read-only location, you'll get a segmentation fault.

For the uninitialized case, const int a , the a needs to be initialized to zero since it's at file scope (or; a is a global variable). The linker then places the variable in the .bss section, together with other data that also is zero initialized at program startup. The .bss section is read/write and you get no segfault when you try to write to it.

All this is not something you can rely on , this could change with minor modification to the code, if you use another compiler or a newer/older version of your compiler etc.

nos
  • 223,662
  • 58
  • 417
  • 506
  • ohh got it :) thankuuu Actually I am preparing for placements so I have to ignore the warnings because this type of questions are generaly asked – A.s. Bhullar Aug 07 '14 at 10:50
  • 1
    No, you really can write standards-compliant code and code that generates no warnings even when doing things that are platform-specific. Serious professional programmers do not ignore warnings. You shouldn't either. – david.pfx Aug 07 '14 at 12:21
6

Global and static variables are initialized implicitly if your code doesn't do it explicitly as mandated by the C standard.

From the doc:

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.

In G++ you will receive the error for the second case ie, const int a;.

6.9.2 External object definitions

Semantics

1 If the declaration of an identifier for an object has file scope and an initializer, the declaration is an external definition for the identifier.

2 A declaration of an identifier for an object that has file scope without an initializer, and without a storage-class specifier or with the storage-class specifier static, constitutes a tentative definition. If a translation unit contains one or more tentative definitions for an identifier, and the translation unit contains no external definition for that identifier, then the behavior is exactly as if the translation unit contains a file scope declaration of that identifier, with the composite type as of the end of the translation unit, with an initializer equal to 0.

Community
  • 1
  • 1
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
1

declares a constant integer variable. It means it’s value can’t be modified. It’s value is initially assigned to 10. If you try to change its value later, the compiler will issue a warning, or an error, depending on your compiler settings.

AtmiyaDas2014
  • 300
  • 1
  • 3
  • 25