C Programming The program result is changed by time(&start) function of <time.h>
Question
Recently I experienced very strange situation by my C program. Usually my program works fine, but if I add just a few lines to check elapsed time, the result changes.
The code of which result changed is:
while (!feof(pfInputFile) && (c = fgetc(pfInputFile)) != EOF){
for(i = 1 ; i < SEED_SIZE ; i++){
pcSeq[i-1] = pcSeq[i]; // Shift left all sequence
}
pcSeq[SEED_SIZE - 1] = c;
}
And the code I added and cause a problem is below:
#include <time.h>
time_t start, end;
time(&start); time(&end);
And then, the characters that are read by the above source code are changed to unrecognized character.
Solution
Not enough details but I'll take a wild guess.
You returned a pointer to a local variable pcSeq
. Then in another function you call time(&start)
with the result that the start
variable now shares the same stack address that pcSeq
had, so it got overwritten.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow