cpuer
cpuer

Reputation: 7793

How's a temporary implemented in C++?

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?

Upvotes: 0

Views: 136

Answers (5)

Hoa Long Tam
Hoa Long Tam

Reputation: 750

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)

Upvotes: 0

geekosaur
geekosaur

Reputation: 61379

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).

Upvotes: 2

R.. GitHub STOP HELPING ICE
R.. GitHub STOP HELPING ICE

Reputation: 215257

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

Upvotes: 2

MRAB
MRAB

Reputation: 20654

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

Upvotes: 0

Related Questions