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;
}
================================================================
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.
Re: Variable used without being initialized?
Great, that worked! I'm new at this, thank you for the walk-through!