# Thread: how to avoid overflow

1. ## how to avoid overflow

I have a code

Code:
```time_to_expire= counter_of_clock + wait_time;

while (time_to_expire > counter_of_clock) {

sleep ( sleep_time);
}```
The “clock of counter” increments automatically every second by the system hardware. and i want to make it wait for a certain time taken as “wait_time” variable.

in this all variables above have exactly the same number of bits. The issue here is when the clock counter reaches to the point when it overflows and overruns to zero. This new value of clock counter is thus less than expire_time and hence the loop will wait for more than expected time.

2. We've already answered you here. Stop asking questions if you're not going to listen to the answers!

Quzah.

3. Hi,

Problem isnt the same here. The issue here is when the clock counter reaches to the point when it overflows and overruns to zero.

For example, starting at the first statement, assuming the values are

clock_counter = 0xFFED //lets a 16 bit counter.

wait_time = 17

sleep_time = 5

So expire_time = 0xFFFE

Considering this in 4th iteration, the while loop would not end as expected rather it will continue for a longer time. Since, on fourth iteration, the clock_counter will overflow and will return its value as 1. That's the problem!

hence the loop will wait for more than expected time.

4. Of course it's the same. You're just not doing it right. Apply the logic of the if check from the previous thread to this problem.
Code:
```time_to_expire= counter_of_clock + sleep_time;

while (time_to_expire > counter_of_clock) {
if( sleep_time > MAX - time_to_expire )
{
resetclockcounter( );
time_to_expire = clock_counter + sleep time;

}
sleep( sleep_time );
}```
Same problem. Same logic. Same solution.

Quzah.

5. Hi,
Some points i have missed in above discussion are... Sorry for missing them.

There is a machine with a read only clock counter. The clock counter increments automatically every second by the system hardware.

Will variables and their type play any role in between?

6. Originally Posted by maven
Will variables and their type play any role in between?
Possibly.

7. What will be the effect of variable and types.. And as counter is read only, how will we be ensure to reset it.

8. You don't reset the hardware timer. You use the hardware timer values to increment your own timer system. So when a specified duration has elapsed, you reset your timer, not the hardware.

Letting you fool around with the internal timer of the system would not be a good idea. In fact back in the DOS days people would hook the wrong interrupt to do timing and it threw their entire system clock out of whack. Solution: reboot.

A timer cannot go on forever and every timer has a finite number of intervals it can represent due to data type and data type binary representation. But a timer does not have to go on forever to do timing.

Follow the advice that has already been given and you have your answer. We could debate the wonders of your timing and what it would happen if we did A to B and so forth, but it doesn't get you any closer to what you want.

Popular pages Recent additions