是什么让机器的较低层保持常量?

发布于 2024-09-29 22:44:32 字数 214 浏览 2 评论 0原文

当在 C++ 中创建 const 的东西时,是什么使得你不能在机器的较低级别隐式地将其传递为非常量?机器如何确定这是const

(除了 const 意味着它的含义这一事实......)

它是否可能存储在内存的 .rdata 部分中,或者是否有一些设置使其成为 const 或者它是如何工作的?

谁能澄清一下吗?

When making something const in C++ what makes it that you cannot for example implicitly pass it a non-const at the lower levels of the machine? How is it determined by the machine that this is const?

(besides the fact that const means what it means...)

Is it perhaps stored in the .rdata section of memory or is there a bit that gets set that makes it const or how does that work?

Can anyone clarify?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

若水般的淡然安静女子 2024-10-06 22:44:32

const 主要是编译时的事情;它并不意味着它们在运行时可能存储在哪里,或者它们是否在运行时受到保护。

实际上,编译器可能会选择将常量放入可执行文件的程序部分,该部分可能会受到内存管理单元(如果存在)的写保护。或者,编译器可以将常量直接折叠到代码中,以便它们甚至不作为可寻址位置存在。

或者,它可能不执行这些操作。

const is mostly a compile-time thing; it doesn't imply anything about where they might be stored at runtime, or whether they might be protected at runtime.

In practice, the compiler may choose to put constants in the program section of the executable, which may be write-protected by the memory-management unit (if it exists). Alternatively, the compiler may fold the constants directly into the code, so that they don't even exist as addressable locations.

Alternatively, it may do none of these things.

转身以后 2024-10-06 22:44:32

const-ness 几乎总是由编译器强制执行,仅此而已。根本没有机器保护。

编辑:@Oli Charlesworth 的答案比我的好。

const-ness is almost always enforced by the compiler, nothing more, nothing less. No machine protection at all.

Edit: @Oli Charlesworth's answer is better than mine.

谎言 2024-10-06 22:44:32

“const”并不一定意味着存储是只读的。这是一个声明,表明 C++ 程序不会更改它(因此编译器应该拒绝任何这样做的尝试)。这并不意味着该值不会改变。

变量“const volatile”是相当合法的(程序不会改变它,但它可能随时改变)。例如,只读硬件端口很可能就是这样一个野兽。

因此,在“机器的较低级别”,无需执行任何操作。内存就是这样,应用程序程序员需要正确地声明内容。

"const" does not necessarily mean the storage is read-only. It is a declaration that the C++ program will not change it (and hence that the compiler should reject any attempt to do so). It doesn't mean the value will not change.

It's quite legitimate for a variable to be "const volatile" (the program won't change it, but it might change at any time). A read-only hardware port might well be such a beast, for example.

So at the "lower levels of the machine", nothing needs to be done. The memory is what it is, and the application programmer needs to declare stuff correctly..

缺⑴份安定 2024-10-06 22:44:32

在 C++ 中,const 很少与硬件有任何关系。它主要是告诉编译器某些类型的访问应该导致编译器错误的一种方式。

唯一的例外是原始或 POD 类型的静态 const 变量,它们通常链接到可执行映像中的只读部分,如果您放弃常量并尝试修改其中之一,则会触发某种页面错误。

In C++, const, rarely has anything to do with hardware. It is mostly a way to tell the compiler that certain kinds of access should result in a compiler error.

The only exception is static const variables of primitive or POD type, which are usually linked into a read-only section in the executable image, and will trigger some kind of page fault if you cast away const-ness and try to modify one of them.

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