0

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
cpuer
  • 7,413
  • 14
  • 35
  • 39

5 Answers5

2

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
R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
  • @R..,what if `cout << "abc"`?If that's still not a temporary,can you give one example? – cpuer May 30 '11 at 02:30
  • oh,I see `b * c` is a temporary,where `b`,`c` are variables. – cpuer May 30 '11 at 02:35
  • It's not a temporary. It's a string literal. It will be assigned a fixed address (usually in the "text" (code) or read-only data segment) and the absolute or relative address of the string will be emitted in the asm to access it. – R.. GitHub STOP HELPING ICE May 30 '11 at 02:36
  • @cpuer: I'm not sure if or where to term "temporary" is defined, but I suspect it only applies to objects of `class` type where an actual object undergoes construction and destruction. – R.. GitHub STOP HELPING ICE May 30 '11 at 02:37
  • @R..,this term is from this answer: http://stackoverflow.com/questions/6171630/why-doesnt-operator-overloading-for-pointers-work/6171694#6171694 – cpuer May 30 '11 at 02:44
  • Ben's use of "temporary" there seems to be a casual way of saying "non-lvalue expression". – R.. GitHub STOP HELPING ICE May 30 '11 at 03:00
  • @R..,does `non-lvalue` equal to `rvalue`? – cpuer May 30 '11 at 03:04
  • Does non-square mean the same thing as rectangle? :-) – R.. GitHub STOP HELPING ICE May 30 '11 at 03:22
  • @R.. ,what can it be if it's neither `lvalue` nor `rvalue`? :) – cpuer May 30 '11 at 03:34
  • I'm not sure about C++, but in C, "rvalue" appears only in a footnote under 6.3.2.1 as a common name for "value of an expression". Similar to the relationship between rectangle and square, some rvalues are also lvalues, and perhaps all lvalues are rvalues, but I would be hesitant to claim this since I'm not familiar with the formal definition of "rvalue" in C++ (if there even is one). – R.. GitHub STOP HELPING ICE May 30 '11 at 03:40
2

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

geekosaur
  • 59,309
  • 11
  • 123
  • 114
0

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

MRAB
  • 20,356
  • 6
  • 40
  • 33
0

See these questions:

Community
  • 1
  • 1
yasouser
  • 5,113
  • 2
  • 27
  • 41
0

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)
Hoa Long Tam
  • 740
  • 3
  • 6