简体   繁体   English

c中的初始化vs未初始化的全局const变量

[英]initialized vs uninitialized global const variable in c

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? 所以我的问题是为什么编译器对于uninitialize和初始化全局const变量的行为不同?

I am using gcc for windows (mingw). 我正在使用gcc for windows(mingw)。

You are modifying a const object, and that is simply undefined behavior - so don't do it, and don't ignore compiler warnings. 您正在修改一个const对象,这只是未定义的行为 - 所以不要这样做,不要忽略编译器警告。

Now, the actual reason for the different behavior in your particular case is that for const int a=10; 现在,在你的特定情况下,不同行为的实际原因是const int a=10; the value 10 has to be stored somewhere. 10必须存储在某处。 Since the variable is const, the linker places it in the .rodata or a similar read only section of the executable. 由于变量是const,链接器将它放在.rodata或可执行文件的类似只读部分。 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). 对于未初始化的情况, const int aa需要初始化为零,因为它位于文件范围(或者; a是全局变量)。 The linker then places the variable in the .bss section, together with other data that also is zero initialized at program startup. 然后,链接器将变量放在.bss部分中,以及在程序启动时也初始化为零的其他数据。 The .bss section is read/write and you get no segfault when you try to write to it. .bss部分是可读/写的,当你尝试写入时,你没有得到段错误。

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. 所有这些都不是你可以依赖的东西,如果你使用另一个编译器或更新/更旧版本的编译器等,这可能会随着对代码的微小修改而改变。

Global and static variables are initialized implicitly if your code doesn't do it explicitly as mandated by the C standard. 如果您的代码没有按照C标准的要求明确执行,则会隐式初始化全局变量和静态变量。

From the doc: 来自doc:

const is a type qualifier. const是一个类型限定符。 The other type qualifier is volatile. 另一种类型限定符是volatile。 The purpose of const is to announce objects that may be placed in read-only memory, and perhaps to increase opportunities for optimization. const的目的是宣布可能放在只读存储器中的对象,并且可能增加优化的机会。

In G++ you will receive the error for the second case ie, const int a; 在G ++中,您将收到第二种情况的错误,即const int a; .

6.9.2 External object definitions 6.9.2外部对象定义

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. 1如果对象的标识符声明具有文件范围和初始值设定项,则声明是标识符的外部定义。

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. 2具有文件范围而没有初始化程序且没有存储类说明符或存储类说明符为静态的对象的标识符声明构成暂定定义。 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. 如果翻译单元包含一个或多个标识符的暂定定义,并且翻译单元不包含该标识符的外部定义,那么行为就像翻译单元包含该标识符的文件范围声明一样,复合类型为翻译单元的结尾,初始化程序等于0。

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. 它的值最初分配给10.如果稍后尝试更改其值,编译器将发出警告或错误,具体取决于您的编译器设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 已初始化与未初始化的全局变量,存储在 RAM 的哪一部分? - Initialized vs uninitialized global variables, in which part of the RAM are the stored? C中全局结构中的常量 - Const variable in a global struct in C 为什么const变量不需要在C中初始化? - Why const variable need not to be initialized in C? const变量何时初始化 - When is const variable initialized 在C ++中声明全局范围变量const - Declare a global scope variable const in c++ 对于每个静态和全局(未初始化/初始化的变量),数据段的内存分配未正确进行 - Memory allocation for Data segment not happening properly for each static and global(uninitialized/ initialized variable) null 初始化全局指针变量的 C 程序的 memory 布局是什么? - What is memory layout of C program for null initialized global pointer variable? 为什么在 C++ 中允许对 const 全局变量进行多重定义,而在 C 中不允许? - Why is multiple definition of a const global variable allowed in C++ and not in C? 在C中,为什么我初始化为0 int变量被“nm”报告为未初始化? - In C, why does my initialized to 0 int variable get reported as uninitialized by “nm”? C未初始化的互斥锁工作和初始化的互斥锁失败? - C uninitialized mutex works and initialized mutex fails?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM