constexpr变量存储在哪里

发布于 2025-02-12 15:45:35 字数 614 浏览 2 评论 0原文

我看到constexpr变量在堆栈溢出上进行了高度讨论。但是没有人谈论的一件事:

ConstexPR变量存储在哪里?

每个人都知道C和C ++程序的内存位置表。

  • 堆栈
  • 静态
  • 文本

操作系统(例如Linux)
对于操作系统,可执行代码和所有静态变量都会从硬盘驱动器盘复制到文本的分配区域,即静态。在公羊中。从那里开始,程序以一个过程开始。

嵌入式系统(例如Atmel控制器)
对于嵌入式系统,这是不同的。在这里,可执行的代码和文字被永久存储在闪存中。只有静态变量被复制到RAM中。

文字或静态区域
#defines相比,我了解constexpr的好处,但是对于嵌入式系统程序员,总有一个性能问题。在嵌入式系统上,RAM是一种昂贵的资源。因此,我需要知道constexpr变量是否存储在文本或静态区域中。或更确切地说,它们是永久存储在闪存中吗?还是真的在嵌入式系统的RAM中创建为变量?

I saw that constexpr variables are highly discussed on stack overflow. But there is one thing no one talks about:

Where are constexpr variables stored?

Everyone knows the memory location tables of C and C++ programs.

  • stack
  • heap
  • static
  • text

Operating Systems (like Linux)
For operating systems, executable code and all the static variables get copied from the hard drive disc into the allocated areas of text, static ect. in the RAM. From there the program starts as a process.

Embedded Systems (like Atmel Controller)
For an embedded system, this is different. Here the executable code and the literals are permanently stored in a flash Memory; only the static Variables get copied into the RAM.

text or static area
I understand the benefits of constexpr compared to #defines, but for an embedded system programmer, there is always the performance question. On embedded systems, RAM is an expensive resource. So, I need to know if constexpr variables get stored in the text or in the static area. Or more precisely, are they permanently stored in the flash memory or do they get really created as variable into the RAM of an embedded system?

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

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

发布评论

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

评论(1

稀香 2025-02-19 15:45:35

constexpr不会更改变量的存储类。因此,constexpr的存在不会更改编译器可以放置变量的地方。

但是,constexpr变量有一些重要特征。任何变量都可以具有这些特征,但是constexpr限定符特别需要:

  1. 不能更改该变量(隐式const)。
  2. 该变量以编译时常数表达式初始化。

鉴于这些事实,编译器可以基本上将存储空间放在任何地方。如果类型与寄存器类型匹配,并且该值的大小足够大,可以通过单个“ Load Literal” Opode加载该寄存器,则编译器可以将变量的所有用途转换为简单地将文字值加载到寄存器中。

当然,如果您开始做诸如获取变量的地址之类的事情,则可能必须占用实际存储。但是,即使这取决于编译器内在的良好状态。

constexpr does not change the storage class of a variable. So the presence of constexpr does not change where the compiler can put the variable.

There are however some important characteristics of constexpr variables. Any variable could have these characteristics, but the constexpr qualifier specifically requires:

  1. The variable cannot be changed (is implicitly const).
  2. The variable is initialized with a compile-time constant expression.

Given these facts, the compiler can put the storage basically anywhere. If the type matches a register type and the value is of sufficient size that it can be loaded via a single "load literal" opcode, the compiler could convert every use of the variable into simply loading the literal value into a register.

Of course, if you start doing things like getting the address of the variable, it may have to take up actual storage. But even that depends on how good the compiler is at inlining.

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