This may be due to my lack of understanding of C, but I'm getting some (what i would call) obscure errors when dealing with arrays:
Here is the code:
int * generateRandomArray(int numPageReplacements) {
// Seed random number generator
srand(time(NULL));
int * randPages = (int *)malloc(sizeof(int)*numPageReplacements);
for (int i = 0; i < numPageReplacements; i++) {
randPages[i] = rand() % 10; // generate (with uniform distribution) number 0 - 9
printf("%d ", randPages[i]); // for testing purposes
}
printf("\nRandomPages[3] = %d \n" ,randPages[3]);
return randPages; // return array pointer
}
Output:
7 9 4 6 4 6 5 7 6 3
RandomPages[3] = 6
Program ended with exit code: 0
If I run this back to back (take random numbers generated above) it will give 4 sometimes (what one would expect) and 6 at others (it's like it doesn't know the bounds of each array cell).
When I attempt to obtain the array from this function from the call:
int * ary = generateRandomArray(int numPageReplacements);
printf("Size of ary = %lu",sizeof(ary));
Output:
Size of ary = 8
It's equal to 8 no matter WHAT numPageReplacements is.
Compiler:
Apple LLVM version 6.0 (clang-600.0.54) (based on LLVM 3.5svn)
Target: x86_64-apple-darwin14.0.0
Thread model: posix
Am I missing something here?