用C const修饰词的深层分析 [英] Deep Analysis of Const Qualifier in C

查看:138
本文介绍了用C 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;  
}  

如果答案是code段再怎么做以下code的工作??

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 ;  
} 

这code正常工作......这怎么可能改变一个只读存储器?它是如何真正得到存储在哪里?请解释给我详细。

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.

推荐答案

关键字常量表示为只读(即不能改变一个变量在运行时)。它并不表示一个编译时间常数因此,所有的变量通常属性的应用。具体地讲,它被分配寻址存储空间。

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的象征申报对象文件中,以便它可以从其他code访问的文件,请记住,常量对象必须用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).

的原因,您张贴作品,是因为单目运算符&放了code段; 可以应用到任何左值,其中包括常量对象。虽然这里的行为是不确定的,我怀疑你的编译器正在检测这种用法,并确保你的常量声明中给出的地址空间,因此不内联它,即使文件中它被宣布。

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.

编辑:同时看到:<一href=\"http://publications.gbdirect.co.uk/c_book/chapter8/const_and_volatile.html\">http://publications.gbdirect.co.uk/c_book/chapter8/const_and_volatile.html

让我们来看看是什么意思时,常量
  用来。这真的很简单:
  常量是指东西是不
  修改的,所以一个数据对象,它是
  声明为const作为一部分的
  类型规范切不可
  在运行期间分配给任何方式
  的方案。它很可能
  对象的定义将
  包含一个初始化(否则,
  因为你不能分配给它,怎么
  它会永远得到的值),但是这
  并非总是如此。例如,
  如果你访问一个硬件端口
  在一个固定的内存地址,并承诺
  只能从中读取,那么这将是
  声明为常量但不
  初始化。搜索结果采取的地址
  一个类型的数据对象,它是不
  const和将其放入一个指向
  的的const限定版
  相同类型是既安全又明确地
  允许的;你将能够使用
  指针检查对象,但不
  修改它。把一个地址
  const型成一个指针
  不合格的类型是更
  危险,因此被禁止
  (尽管你可以解决这个问题
  用cast)。例如...

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天全站免登陆