For example, I have the following for loop:
for (int i = 0; i < 5; i ){
for (int j = 0; j < 5; j ){
int a = i j;
}
}
where is the value of i in each iteration stored? is it automatically stored in the cache?
CodePudding user response:
The C language is specified by the C standard using an imaginary computer that literally does the things described in the C standard. In this abstract computer, defining i with int i = 0 reserves some bytes in memory to hold the value of i. Throughout the loop, the value of i is in that memory. There is no cache specified for this abstract computer, just some sort of memory that is not physically described.
When a compiler translates the C program to assembly language, the C standard allows the compiler to create any assembly language that produces the same observable results that the abstract computer would. In the translated program, the value of i might be in memory, it might be in a processor register, or it might be removed from the program entirely if the compiler figures out a way to compute the same results without using i. If it is in memory, the processor might store it in cache too. For the most part, processors cache data from memory automatically, without direct intervention from the program.
