Variable used without being initialized?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Variable used without being initialized?

  1. #1
    Join Date
    Aug 2004
    Posts
    60

    Variable used without being initialized?

    Why would this code be throwing the "run-time check failure #3 - the variable 'SQLStrtTime' variable is being used without being initialized" error?

    ===============================================================
    UINT SQL_Thread(LPVOID param)
    {
    LARGE_INTEGER SQLFreq;
    LARGE_INTEGER SQLStrtTime;
    LARGE_INTEGER SQLCurrTime;
    double SQLDiff;
    bool SQLInitTimer1;



    for( ;; )
    {

    // Read/Write SQL Data From/To Database (every 500ms)
    if (SQLInitTimer1 = false)
    {
    QueryPerformanceCounter(&SQLStrtTime);
    SQLInitTimer1 = true;
    }
    else if (SQLInitTimer1 = true)
    {
    QueryPerformanceCounter(&SQLCurrTime);
    QueryPerformanceFrequency(&SQLFreq);
    SQLDiff = ((double)(SQLCurrTime.QuadPart - SQLStrtTime.QuadPart) / (double)(SQLFreq.QuadPart)) * 1000;
    if (SQLDiff >= 500)
    {
    UpdateSQL();
    SQLInitTimer1 = false;
    }
    }
    }

    return 0;

    }

    ================================================================

  2. #2
    Join Date
    Jan 2002
    Location
    Houston, TX
    Posts
    1,421

    Re: Variable used without being initialized?

    A couple of things I notice about your code...

    First, in your if
    Code:
    if (SQLInitTimer1 = false)
    {
    	QueryPerformanceCounter(&SQLStrtTime);
    	SQLInitTimer1 = true;
    }
    you really need
    Code:
    if (SQLInitTimer1 == false)
    and instead of
    Code:
    else if (SQLInitTimer1 = true)
    you need
    Code:
    else if (SQLInitTimer1 == true)
    Secondly, in this block
    Code:
    if (SQLInitTimer1 == false)
    {
    	QueryPerformanceCounter(&SQLStrtTime);
    	SQLInitTimer1 = true;
    }
    else if (SQLInitTimer1 == true)
    {
    	QueryPerformanceCounter(&SQLCurrTime);
    	QueryPerformanceFrequency(&SQLFreq);
    	SQLDiff = ((double)(SQLCurrTime.QuadPart - SQLStrtTime.QuadPart) / (double)(SQLFreq.QuadPart)) * 1000; 
    	if (SQLDiff >= 500)
    	{
    		UpdateSQL();
    		SQLInitTimer1 = false;
    	}
    }
    If the initial state of SQLInitTime1 is not false, you will fall directly into your else if and SQLStartTime has not yet been initialized.

    Hope that helps.

    PS - Indentation and the use of code tags would really help you see the flow.
    Be sure to rate those who help!
    -------------------------------------------------------------
    Karl - WK5M
    PP-ASEL-IA (N43CS)
    PGP Key: 0xDB02E193
    PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193

  3. #3
    Join Date
    Aug 2004
    Posts
    60

    Re: Variable used without being initialized?

    Great, that worked! I'm new at this, thank you for the walk-through!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center