Reputation: 12381
As all we know, in a modern architecture of computer, there are L1/L2/L3 cache, which can improve the performance of execution of a program.
My question is if we can know if a variable is loaded into L1/L2/L3 cache while executing?
We know that we could print the address of a variable in C++ like this: int i; cout << &i;
. So if the i
is loaded into the L1 cache, which kind of address is shown by the &i
? the address of L1 cache or the address of the RAM?
Upvotes: 1
Views: 1752
Reputation: 2275
In a typical PC, &i
is always the address of a RAM location. This is because the cache is essentially transparent to the programmer. It simply stores a copy of the same information contained in a region of memory, at a place that can be more quickly accessed by the CPU. It is not addressable as a memory location directly.
As for whether the variable is 'located' in the cache while the code is executing, it most certainly is. When you try to access data in a memory location, if it is not already in the cache, it will be brought into the cache first before your request is served.
Upvotes: 2