“const”如何表示?实施的?

发布于 2024-12-12 09:40:06 字数 356 浏览 0 评论 0原文

C 或 C++ 编译器(例如 gcc)如何遵循 const 声明?

例如,在下面的代码中,编译器如何跟踪变量ciconst并且不能被修改?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

泪冰清 2024-12-19 09:40:06

与变量的任何其他符号信息(地址、类型等)非常相似。通常有一个符号表存储有关声明的标识符的信息,每当遇到对该标识符的另一个引用时都会查阅该符号表。

(一个更有趣的问题是,这些知识是否仅在编译期间甚至在运行时存在。大多数语言在编译后丢弃符号表,因此即使标识符被声明为“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.)

风流物 2024-12-19 09:40:06

当然,这取决于每个编译器的实现,但简而言之,它将变量的 const 和 volatile 限定符(如果有)与其他变量一起存储在其变量符号表中。诸如变量的类型以及它是否是指针/引用之类的信息。

Of course it is up the implementation of each compiler, but in a nutshell, it stores the variable's const and volatile 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.

腻橙味 2024-12-19 09:40:06

正如其他人所说,编译器跟踪 const 的方式与编译器跟踪变量是 int 的事实相同。事实上,我读过,至少 gcc 认为 const int 是与 int 不同的类型,因此它甚至不被作为修饰符进行跟踪,它的跟踪方式与int

请注意,您实际上可以通过指针转换来更改 const 的值,但结果是未定义的:

#include <stdio.h>
#include <stdlib.h>

int main(void) {
        const int c = 0;
        printf("%d\n", c);
        ++*(int*)&c;
        printf("%d\n", c);
}

在我使用 gcc 的机器上,此打印

0
1

但是用 g++ 编译给出

0
0

As others have said, const is tracked by the compiler the same way the compiler tracks the fact that a variable is an int. In fact, I have read that at least gcc considers const int a distinct type from int, so it's not even tracked as a modifier, it's tracked the exact same way as an int.

Note that you actually can change the value of a const via pointer casting, but the result is undefined:

#include <stdio.h>
#include <stdlib.h>

int main(void) {
        const int c = 0;
        printf("%d\n", c);
        ++*(int*)&c;
        printf("%d\n", c);
}

On my machine using gcc, this prints

0
1

But compiling with g++ gives

0
0
左秋 2024-12-19 09:40:06

我相信其他人可以详细说明,但简而言之,是的。编译器跟踪所有类型说明符,以便它知道 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".

触ぅ动初心 2024-12-19 09:40:06

这行告诉了它

const int ci = get_foo();

编译器知道 ciconst,因为您用As you pass ci around to other function or allocate it to othervariables ,编译器通过阻止您执行任何可能更改其值的操作来保留常量性。

例如,以下内容会产生编译器错误。

int *ci2 = &ci;
(*ci2)++;

因为编译器不会让你修改ci的值。

The compiler knows that ci is const because you told it so with the line

const int ci = get_foo();

As 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.

int *ci2 = &ci;
(*ci2)++;

Because the compiler won't let you modify the value of ci.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文