mercredi 25 mars 2015

C array corrupted after calling a function

I have a simple C program which is supposed to fill an array with random values (this is the beginning of a school assignment). I'm using rand(), seeding it with srand(time()). Because this tended to generate somewhat predictable results when run repeatedly in a certain time period, I made another very simple function to give me a somewhat more random seed value:



time_t getRandomSeed(int numberOfIterations)
{
int i;
time_t result=time();

for (i = 0; i < numberOfIterations; i++)
{
result = randomizeSeed(result);
}

return result;
}

time_t randomizeSeed(time_t seed)
{
time_t result, fullTime, Modifier1, Modifier2;

srand((unsigned int)seed);
fullTime = time();
Modifier1 = time() >> (rand() % 10);
Modifier2 = (unsigned short)time();
Modifier2 <<= (rand() % 9 + 9);

result = fullTime ^ Modifier1 ^ Modifier2;

return result;
}


In main, I'm calling this inside srand() after declaring 2 int arrays. When I return to main, these arrays are garbage, and I can't figure out why.



int main(void)
{
int randomNumbers[ARRAY_LENGTH];
int sortedNumbers[ARRAY_LENGTH];

srand((unsigned int)getRandomSeed(2));

fillArray(randomNumbers, sizeof(randomNumbers) / sizeof(randomNumbers[0]));

return 0;
}


I set a breakpoint at line 4, and I have a perfectly normal unititialized array (at 0x00e1ea70 in the most recent run). Then I set another breakpoint at line 8, so I could see the array just before it was passed into fillArray. At this point, the address of the array is somehow 0xfffff04c, and all the elements show "unable to read memory", which I guess makes sense given the address is garbage.


Can anybody give me a clue what's happening to my array?


*Note, this is for a school assignment, so I don't really care if the RNG is secure, nor can I use any 3rd party libraries.





Aucun commentaire:

Enregistrer un commentaire