“const”如何表示?实施的?
C 或 C++ 编译器(例如 gcc)如何遵循 const
声明?
例如,在下面的代码中,编译器如何跟踪变量ci
是const
并且不能被修改?
int
get_foo() {
return 42;
}
void
test()
{
int i = get_foo();
i += 5;
const int ci = get_foo();
// ci += 7; // compile error: assignment of read-only variable ?ci?
}
How does a compiler, C or C++, (for example, gcc) honors the const
declaration?
For example, in the following code, how does the compiler keeps track that the variable ci
is const
and cannot be modified?
int
get_foo() {
return 42;
}
void
test()
{
int i = get_foo();
i += 5;
const int ci = get_foo();
// ci += 7; // compile error: assignment of read-only variable ?ci?
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
与变量的任何其他符号信息(地址、类型等)非常相似。通常有一个符号表存储有关声明的标识符的信息,每当遇到对该标识符的另一个引用时都会查阅该符号表。
(一个更有趣的问题是,这些知识是否仅在编译期间甚至在运行时存在。大多数语言在编译后丢弃符号表,因此即使标识符被声明为“const”,您也可以操作编译后的代码并强制使用新值。需要复杂的运行时系统(和代码签名)来防止这种情况发生。)
Much like any other bit of symbol information for variables (address, type etc.). Usually there is a symbol table which stores information about declared identifiers, and this is consulted whenever it encounters another reference to the identifier.
(A more interesting question is whether this knowledge is only present during compilation or even during runtime. Most languages discard the symbol table after compiling, so you can manipulate the compiled code and force a new value even if the identifier was declared 'const'. It takes a sophisticated runtime system (and code signing) to prevent that.)
当然,这取决于每个编译器的实现,但简而言之,它将变量的 const 和 volatile 限定符(如果有)与其他变量一起存储在其变量符号表中。诸如变量的类型以及它是否是指针/引用之类的信息。
Of course it is up the implementation of each compiler, but in a nutshell, it stores the variable's
const
andvolatile
qualifiers (if any) in its variable symbol table along with other information such as the variable's type and whether or not it is a pointer / reference.正如其他人所说,编译器跟踪 const 的方式与编译器跟踪变量是 int 的事实相同。事实上,我读过,至少 gcc 认为 const int 是与 int 不同的类型,因此它甚至不被作为修饰符进行跟踪,它的跟踪方式与
int
。请注意,您实际上可以通过指针转换来更改
const
的值,但结果是未定义的:在我使用 gcc 的机器上,此打印
但是用 g++ 编译给出
As others have said,
const
is tracked by the compiler the same way the compiler tracks the fact that a variable is anint
. In fact, I have read that at least gcc considersconst int
a distinct type fromint
, so it's not even tracked as a modifier, it's tracked the exact same way as anint
.Note that you actually can change the value of a
const
via pointer casting, but the result is undefined:On my machine using gcc, this prints
But compiling with g++ gives
我相信其他人可以详细说明,但简而言之,是的。编译器跟踪所有类型说明符,以便它知道 ci 的类型是“const int”,而不是“int”。
I'm sure others can elaborate more, but in short, yes. The compiler keeps track of all type specifiers so that it knows that ci is of type "const int", and not "int".
这行告诉了它
编译器知道
ci
是const
,因为您用As you passci
around to other function or allocate it to othervariables ,编译器通过阻止您执行任何可能更改其值的操作来保留常量性。例如,以下内容会产生编译器错误。
因为编译器不会让你修改ci的值。
The compiler knows that
ci
isconst
because you told it so with the lineAs you pass
ci
around to other functions or assign it to other variables, the compiler preserves that const-ness by preventing you from doing anything that could potentially change its value.For example, the following produces a compiler error.
Because the compiler won't let you modify the value of ci.