Originally Posted by
tra86
Consider the following:
Code:
int i1 = 1;
char *pc = (char*)&i1;
int i2 = *(int*)pc;
This example has overhead, yes.
As soon as you take the address of a variable it must be stored in memory. So if i1 was in a register, it's not anymore.
Then the final line must reload from the address of pc. This is because the contents of that memory address could have changed by another process/thead since we wrote to it.
You're also declaring new pointer variables which need to be stored somewhere, and that'll have register usage or stack cost.
More generally:
Code:
int i = 1;
char c = 2;
i = (int)c; // overhead -- value of c must be sign extended
c = (char)i; // overhead -- value of i must have top bits masked
I think for this example it is possible for the compiler to optimise and assign the constant 2 to both variables.