Originally Posted by
Nwb
But usually where are they stored? What about 'string literals', they're technically lvalues right, so where are they stored?
As laserlight said, it depends on the implementation. And it depends on the level of optimization as well. For exemple, in fragment of code like this, for x86-64:
Code:
char s[] = "flp"; // local var
1. The compiler can store the string in a register:
Code:
mov edx,0x706c66 ; 'flp\0', little endian
2. In the stack:
Code:
sub rsp,8 ; reserve space for the string (qword aligned)
mov dword [rsp],0x706c66
...
add rsp,8 ; take allocated space back.
3. Or in the .rodata section:
Code:
section .rodata
s: db 'flp',0
section .text
... initialize local var on a register ...
mov edx,[s]
4. Or the same variation, using the stack... (reservind space, copying the dword, etc).
For "primitive" types like char, int, float, double... the compiler prefers to use registers, but, sometimes, stack allocated spaces are used.
In other architectures the rules are different, of course... Well... the compiler knows what to do.
PS: Regarding the compiler preference for registers as storage, some people thinks the 'register' keyword forces the use of registers for a local variables. This isn't the case, necessarily:
Code:
// local vars!
int a;
register int b;
Here, the compiler will, probably, allocate both a and b in registers, unless the code tries to get the address-of a. (this way it have no option but to allocate a on stack). The 'register' modifier tells the compiler that it cannot get the address-of b (compilation fails with an error message!).