如何在 C 中实现临时的?
下面的'a'
是一个临时的。
cout << 'a';
它不会在数据部分中恢复(const/static
会),也不应该在堆栈中(局部变量会)。它在哪里?
更新
非左值
和右值
是同一件事吗?
In the following 'a'
is a temporary.
cout << 'a';
It isn't restored in the data section (const/static
does) and shouldn't be in the stack (local variable does). Where is it?
UPDATE
Are non-lvalue
and rvalue
the same thing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
除非你有一个非常糟糕的编译器或机器架构,否则
'a'
不会在任何地方作为数据存储。它是汇编中的立即操作数,例如Unless you have a really horrible compiler or machine architecture,
'a'
is not stored as data anywhere. It's an immediate operand in the asm, e.g.通常与代码内联;大多数现代 CPU 都有一个“立即移动”(学究式的,相对于 PC 的)指令,尽管一些较旧的 CPU 只能从指定的内存地址移动(这就是为什么 Fortran 是用这种假设编写的,导致必须实际分配内存的常量) 。
Inline with the code, usually; most modern CPUs have a "move immediate" (pedantically, PC-relative) instruction, although some older CPUs could only move from specified memory addresses (which is why Fortran was written with that assumption, resulting in constants having to actually be allocated memory).
它可能是其中一条指令的操作数。
It's probably the operand of one of the instructions.
请参阅以下问题:
See these questions:
存储位置取决于您的编译器和架构。
'a'
通常是一个值为 97 的 8 位数量。根据特定体系结构的调用约定,它将被压入堆栈或在过程之前移入寄存器<调用 code>operator<<(ostream&, char) 。然而,这与在当前作用域中存储'a'
无关,而是在被调用者中设置char
类型参数的值;'a'
永远不会存储在当前作用域中。在大多数体系结构上,这可以通过一两个汇编指令来完成,并且不需要存储在静态段、堆或堆栈中(除非参数在堆栈上传递)——只需一两条指令中的几个位。例如:
IA-32:
MIPS-32:
Where this will be stored depends on your compiler and your architecture.
'a'
is generally an 8-bit quantity with the value 97. Depending on the calling convention of your particular architecture, it will either be pushed on the stack or moved into a register just before the procedureoperator<<(ostream&, char)
is called. However, this has nothing to do with storing'a'
in the current scope, but setting the value of thechar
-type parameter in the callee;'a'
is never stored in the current scope. This can be done in one or two assembly instructions on most architectures and doesn't require storage in the static segment, heap or stack (unless parameters are passed on the stack) -- just a few bits in an instruction or two.For example:
IA-32:
MIPS-32: