Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does C(++) do with values that aren't stored in variables?

I'm a bit curious about how C and C++ handle data which isn't stored in variables, e.g:

int IE6_Bugs = 12345;
int Win_Bugs = 56789;

Yeah - everything clear. IE6_Bugs has 123456 stored at it's specific memory address.

Then what about..

if ( IE6_Bugs + Win_Bugs > 10000 )
{
  // ...

So C grabs the values of the two variables and adds them in order to compare the result to the int on the right.

But:

  • Does IE6_Bugs+Win_Bugs ever reach RAM? Or does the processor directly compare the values via its own cache?

  • Or is, in the compiling process, the above if statement converted to something more "understandable" for the machine? (Maybe calculate IE6_Bugs+Win_Bugs first and store it in some variable,...)

like image 682
lamas Avatar asked Jan 30 '10 13:01

lamas


People also ask

What is the value of a variable if it is not assigned a value?

If you do not assign a value to a variable, either by an argument passed to the routine or by a LET statement, the variable has an undefined value. An undefined value is different from a NULL value. If you attempt to use a variable with an undefined value within the SPL routine, you receive an error.

Where are variable values stored in C?

The static variables are stored in the data segment of the memory. The data segment is a part of the virtual address space of a program. All the static variables that do not have an explicit initialization or are initialized to zero are stored in the uninitialized data segment( also known as the BSS segment).

How are values stored in C?

In C the character values are also stored as integers. In the following code, we shall put 270 into a character type data. So the binary equivalent of 270 is 100001110, but takes only first 8-bits from right. So the result will be (00001110), that is 14.

Where variables are stored?

Variables are usually stored in RAM. This is either on the heap (e.g. all global variables will usually go there) or on the stack (all variables declared within a method/function usually go there). Stack and Heap are both RAM, just different locations. Pointers have different rules.


1 Answers

It'll be placed in a register in the CPU (assuming one is available). A register is a sort of super-fast super-small RAM that's built into the CPU itself and used to store results of intermediate operations.

If the value can be determined to always equal xxx then a smart compiler will substitute the value of xxx in its place.

Keep in mind that regardless of whether it is as an expression or a number, (x+y vs 10) it will still need to be placed in a register so that the CPU can access it and perform an operation based on its value.

For more info, read up on computer architecture.

like image 154
Mahmoud Al-Qudsi Avatar answered Sep 22 '22 04:09

Mahmoud Al-Qudsi