RAM vs Cache & Registers

This is a discussion on RAM vs Cache & Registers within the C++ Programming forums, part of the General Programming Boards category; Does the compiler typically automatically use RAM, or do you have to explicitly tell it to use RAM? Does using ...

It will use registers for performing operations (there is no other option). The processor will automatically cache the things in RAM which it thinks will be accessed next. Any part of your program that doesn't fit in the registers (which will be most of it) will go to the RAM, or virtual memory. It's kinda OS dependent... but no, you don't need to worry about it when you're coding.

Register & cache use is part of the optimization process, some compilers will try to put as much as possible in the register and let the memory as much as possible in the cache when it is necessary to limit the acces to the RAM.

To further confuse you, the volitile keyword can be used to force the compiler to place something that it might be tempted to ignore or use a register for in ram. For example

Code:

int max = 8;
for(int i=0; i < max; ++i) ...

here, as long as the compiler sees that max is never modified in the body of the for loop the compiler might very well simply compare i to 8 at every step of the loop, and might even unroll the loop for you (duplicateing the body of the loop and loop over both 4 times)

volitile int max = 8;

forces the compiler to name a location in ram "max" and read this location every time through the loop. Normally we don't care where things live, this is exactly the reason we are using a compiler rather than writing assembly language really, computers are really good at making these sorts of decisions.