constexpr变量存储在哪里
我看到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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
constexpr
不会更改变量的存储类。因此,constexpr
的存在不会更改编译器可以放置变量的地方。但是,
constexpr
变量有一些重要特征。任何变量都可以具有这些特征,但是constexpr
限定符特别需要:const
)。鉴于这些事实,编译器可以基本上将存储空间放在任何地方。如果类型与寄存器类型匹配,并且该值的大小足够大,可以通过单个“ Load Literal” Opode加载该寄存器,则编译器可以将变量的所有用途转换为简单地将文字值加载到寄存器中。
当然,如果您开始做诸如获取变量的地址之类的事情,则可能必须占用实际存储。但是,即使这取决于编译器内在的良好状态。
constexpr
does not change the storage class of a variable. So the presence ofconstexpr
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 theconstexpr
qualifier specifically requires:const
).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.