Skip to main content
gamedev.net
🔒 Locked

Is my code correct?

Started by Sand_Hawk 5 replies 500+ views
Original Post
Sand_Hawk
Sand_Hawk
I wrote some code wich I cannot test myself, the reason is, I can just let my PC running for 49.7 days. So, I want to post it here so you guys can tell me if it''s ok: This is part of a timer class, I want to check in particulair the code underneath the part Timer wrapped around:
  
bool cTimer::Check()
{
    DWORD ulCurrentTime = GetTickCount();

    if (bRunning)
    {
        // Timer wrapped around

        if (ulCurrentTime < ulStartTime)
        {
            DWORD ulPassed = ULONG_MAX - ulStartTime + ulCurrentTime;
            if (ulLimit < ulPassed)
            {
                bRunning = false;
                return (true);
            }

            // Timer isn''t done yet

            return (false);
        }
        else
        {
            if (ulStartTime + ulLimit < ulCurrentTime)
            {
                bRunning = false;
                return (true);
            }

            // Timer isn''t done yet

            return (false);
        }
    }

    // Timer is not running, return true

    return (true);
}
  
---------------- -Earth is 98% full. Please delete anybody you can.
My Site
----------------(Inspired by Pouya)
Teodric
Teodric
Ahem.... debugger? I''d trust what I saw in a debugger over what people said after reading the code, any day.

(step past GetTickCount, change ulCurrentTime, see what happens)

Or maybe I''m missing something obvious?
Sand_Hawk
Sand_Hawk
You''re missing something obvious. According to the MSDN, GetTickCount() wraps around EVERY 49.7 days. So, it goes back to 0 every 50 days. However, I can''t just test this by waiting 49.7 days and then ending up in being not @ my PC when the wrap around happens. So, my question was if anyone is seeing a logical error in there.

Sand Hawk

----------------
-Earth is 98% full. Please delete anybody you can.


My Site
----------------(Inspired by Pouya)
Teodric
Teodric
And what I meant was, use a debugger to set the variable you copy the result from GetTickCount into, and see for yourself what happens. If there is a logic error, it should show itself.
Sand_Hawk
Sand_Hawk
AP: Not mine, but there are pc's out there running windows be have an uptime of more then 50 days. Programming isn't just making things work, it's also about making sure possible pieces of code keep working under certain circumstances(sp?). It's like programming a game and thinking that everyone has working sound, there are still pcs out there with no sound.

[EDIT]
It seems like the MSDN is mistaken, GetTickCount wraps around every midnight(It seems, since the value 3xxxxxxxxxx, same length as ULONG_MAX). So, this is a rather delicate in wich the programmer could mess up pretty hard if not taken into consideration. So, I tested stuff and it seems like my code is correct.
[/EDIT]

Sand Hawk

----------------
-Earth is 98% full. Please delete anybody you can.


My Site


[edited by - Sand_Hawk on December 3, 2002 4:33:22 PM]
----------------(Inspired by Pouya)
Teodric
Teodric
Quick check on my system using GetTickCount says it''s been up 6 days, 5 hours, 46 minutes and some seconds (tick 539174732), so my box at least doesn''t roll around at midnight. That''s on win2k. Perhaps GetTickCount behavior varies with platform, I dunno (no mention in any MSDN source I checked, incl www version).

(reservation for sloppy math with the ticks but the result seems correct, I think I reset my machine a few days back).

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.