如何在 C 中实现临时的?

发布于 2024-11-10 13:05:32 字数 227 浏览 2 评论 0原文

下面的'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 技术交流群。

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

发布评论

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

评论(5

权谋诡计 2024-11-17 13:05:32

除非你有一个非常糟糕的编译器或机器架构,否则 'a' 不会在任何地方作为数据存储。它是汇编中的立即操作数,例如

mov $0x97, %eax

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.

mov $0x97, %eax
熟人话多 2024-11-17 13:05:32

通常与代码内联;大多数现代 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).

素年丶 2024-11-17 13:05:32

它可能是其中一条指令的操作数。

It's probably the operand of one of the instructions.

岁月静好 2024-11-17 13:05:32

存储位置取决于您的编译器和架构。 'a' 通常是一个值为 97 的 8 位数量。根据特定体系结构的调用约定,它将被压入堆栈或在过程之前移入寄存器<调用 code>operator<<(ostream&, char) 。然而,这与在当前作用域中存储'a'无关,而是在被调用者中设置char类型参数的值; 'a' 永远不会存储在当前作用域中。在大多数体系结构上,这可以通过一两个汇编指令来完成,并且不需要存储在静态段、堆或堆栈中(除非参数在堆栈上传递)——只需一两条指令中的几个位。

例如:

IA-32:

    pushl $0x61
    pushl ...     # address of cout
    call  ...     # address of operator<<(ostream&, char)

MIPS-32:

    addiu $a0, $zero, 0x61
    addiu $a1, $zero, ...  # address of cout
    jal   ...              # address of operator<<(ostream&, char)

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 procedure operator<<(ostream&, char) is called. However, this has nothing to do with storing 'a' in the current scope, but setting the value of the char-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:

    pushl $0x61
    pushl ...     # address of cout
    call  ...     # address of operator<<(ostream&, char)

MIPS-32:

    addiu $a0, $zero, 0x61
    addiu $a1, $zero, ...  # address of cout
    jal   ...              # address of operator<<(ostream&, char)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文