C语言中const限定符的深入分析 [英] Deep Analysis of Const Qualifier in C

查看:17
本文介绍了C语言中const限定符的深入分析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

const 变量准确地存储在哪里以及它的行为如何变化?比如说:

Where does a const variable gets stored exactly and how does it behaviour change? Say for example:

const int i=10; // stores where ?  
main()  
{  
   const int j=20; //stores where?   
   return 0;  
}  

如果答案是代码段,那么下面的代码是如何工作的?

If the answer is Code segment then how does the following code work??

main()  
{  
   const int j=20;  
   int *p;  
   p=&j;  
   (*p)++;   
   return 0 ;  
} 

这段代码运行良好...如何更改只读存储器?它是如何真正存储的?请给我详细解释一下.

This code works fine... How it is possible to change a read only memory? How does it really get stored? Please explain it to me in detail.

推荐答案

关键字 const 表示一个只读变量(即不能在运行时更改).它并不表示编译时常量.因此,变量的所有常用属性都适用;具体来说,它被分配了可寻址的存储空间.

The keyword const indicates a variable that is read-only (i.e., cannot be changed at run-time). It does not indicate a compile-time constant. Therefore, all of the usual attributes of variables apply; specifically, it is allocated addressable storage space.

#define 不同,您的常量不一定被编译器内联.相反,编译器将在目标文件中创建一个与您的 const 声明相对应的符号,以便可以从其他代码文件访问它——请记住,const 对象具有外部链接C 中的默认值(尽管某些编译器仍会将常量值内联到定义它的文件中).

Unlike with #define, your constant is not necessarily inlined by the compiler. Rather, the compiler will create a symbol corresponding to your const declaration in the object file so that it can be accessed from other code files—remember that const objects have external linkage by default in C (although some compilers will still inline the constant value within the file where it is defined).

您发布有效"的代码片段的原因是一元运算符 & 可以应用于任何左值,其中包括 const 对象.尽管此处的行为未定义,但我怀疑您的编译器正在检测这种用法并确保为您的 const 声明提供地址空间,因此即使在声明它的文件中也不内联它.

The reason the code snippet that you posted "works" is because the unary operator & can be applied to any lvalue, which includes a const object. Though the behavior here is undefined, I suspect that your compiler is detecting this usage and ensuring that your const declaration is given address space, and therefore not inlining it, even within the file it is declared.

另见:http://Publications.gbdirect.co.uk/c_book/chapter8/const_and_volatile.html

我们来看看const是什么意思用来.这真的很简单:const 表示某事不是可修改的,因此是一个数据对象用 const 作为其一部分声明类型规范不得在运行期间以任何方式分配给的一个程序.很有可能是对象的定义将包含一个初始化程序(否则,既然你不能分配给它,如何它会得到一个价值吗?),但这并非总是如此.例如,如果您正在访问硬件端口在一个固定的内存地址并承诺只是从中读取,那么它将是声明为 const 但不是已初始化.

取一个地址类型的数据对象不是const 并将其放入指向的 const 限定版本相同类型既安全又显式允许;您将能够使用检查对象的指针,但不是修改它.把一个地址const 类型转换为指向的指针不合格的类型更多危险,因此被禁止(虽然你可以通过使用演员表).比如……

Let's look at what is meant when const is used. It's really quite simple: const means that something is not modifiable, so a data object that is declared with const as a part of its type specification must not be assigned to in any way during the run of a program. It is very likely that the definition of the object will contain an initializer (otherwise, since you can't assign to it, how would it ever get a value?), but this is not always the case. For example, if you were accessing a hardware port at a fixed memory address and promised only to read from it, then it would be declared to be const but not initialized.

Taking the address of a data object of a type which isn't const and putting it into a pointer to the const-qualified version of the same type is both safe and explicitly permitted; you will be able to use the pointer to inspect the object, but not modify it. Putting the address of a const type into a pointer to the unqualified type is much more dangerous and consequently prohibited (although you can get around this by using a cast). For example...

这篇关于C语言中const限定符的深入分析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆