Thread: how to avoid overflow

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    136

    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.
    S_ccess is waiting for u. Go Ahead, put u there.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    We've already answered you here. Stop asking questions if you're not going to listen to the answers!


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    136

    Question

    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.
    S_ccess is waiting for u. Go Ahead, put u there.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Dec 2005
    Posts
    136

    Exclamation

    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?
    S_ccess is waiting for u. Go Ahead, put u there.

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by maven
    Will variables and their type play any role in between?
    Possibly.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Registered User
    Join Date
    Dec 2005
    Posts
    136

    Exclamation

    What will be the effect of variable and types.. And as counter is read only, how will we be ensure to reset it.
    S_ccess is waiting for u. Go Ahead, put u there.

  8. #8
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    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 subscribe to a feed

Similar Threads

  1. Stack overflow errors in 3 areas
    By ulillillia in forum C Programming
    Replies: 13
    Last Post: 04-29-2007, 03:20 PM
  2. Signed Char Overflow
    By coder8137 in forum C Programming
    Replies: 5
    Last Post: 11-17-2006, 08:25 AM
  3. large program code ,please help
    By Ash1981 in forum C Programming
    Replies: 14
    Last Post: 01-30-2006, 06:16 AM
  4. How to avoid buffer overflow at input?
    By netstar in forum C++ Programming
    Replies: 4
    Last Post: 02-13-2005, 01:58 AM
  5. Replies: 7
    Last Post: 01-02-2002, 09:16 PM