When accessing data in my program, what exactly will be put in cache?


This Content is from Stack Overflow. Question asked by Abdulmalek Almkainzi

I always hear that when accessing a member in an array, the CPU will put in cache the rest of the array (or a part of it). But how does it know what to put in cache and where I started in the array?

for example:

   int arr[100];
   //fill array with 0-99
   for(int i = 0; i < 100; i++) print(arr[i]);
   //clear cache
   for(int i = 99; i >= 0; i--) print(arr[i]);

will the second loop result in cache misses since the CPU caches ahead only? or does it cache left and right?


Check the Answers

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?