Search code examples
cvariables

uninitialized DWORD variable give random value?


void Encrypt( FileContent *pFile )
{
    /* Get the key total ascii value */
    DWORD asciiKeyValue;
    
    for (DWORD i=0; i < pFile->keyLength; i++)
    {
        asciiKeyValue += pFile->encKey[i];
    }
    
    _tprintf(_T("[*]DEBUG The encKey ascii value is: %ld\n"), asciiKeyValue);
}

I am get an out put of 430 when DWORD asciiKeyValue, but once DWORD asciiKeyValue = 0 is 230 which is as it should be.

Somebody has an explanation for that? Is the asciiKeyValue variable get a random value when it hasn't assigned an explicit value ?


Solution

  • It doesn't "become" random. When you declare your DWORD it has whatever was in the memory at its address before. The memory is not cleaned unless you initialise it to something (like your 0).