I have the beginnings of a C program to simulate Cache, but I am running into a confusing problem. First I have a function to generate the cache:
int * generateCache(int cacheSize){
int cache[cacheSize];
for(int cacheIndex = 0; cacheIndex < cacheSize-1; cacheIndex++){
cache[cacheIndex] = -1;
}
return cache;
}
And in my main function is this:
int main(){
int cacheSize = 16;
int * cache = generateCache(cacheSize);
for(int i = 0; i < cacheSize; i++){
printf("%d %d\n", i, cache[i]);
}
return 0;
}
But my output is this:
warning: address of stack memory associated with local variable 'cache' returned [-Wreturn-stack-address]
return cache;
1 warning generated.
0 -1
1 -1
2 -1
3 -1
4 -1
5 -1
6 -1
7 -1
8 -1
9 -1
10 -1
11 -1
12 0
13 0
14 0
15 0
Can anyone tell me why the last 4 indexes are initialized to 0? This happens regardless of what I initialize the first 12 indexes to; the last 4 are always 0. Thank you.