tags:

views:

72

answers:

2

Im using:

bool GetOS(LPTSTR pszOS)
{
   OSVERSIONINFOEX osve;
   BOOL bOsVersionInfoEx;

   ZeroMemory(&osve, sizeof(OSVERSIONINFOEX));

   osve.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);

   if( !(bOsVersionInfoEx = GetVersionEx ((OSVERSIONINFO *) &osve)) )
      return false;

   TCHAR buf[80];
   StringCchPrintf( buf, 80, TEXT("%u.%u.%u.%u"),
      osve.dwPlatformId,
      osve.dwMajorVersion,
      osve.dwMinorVersion,
      osve.dwBuildNumber);

   StringCchCat(pszOS, BUFSIZE, buf);

   return true;
}

to get the windows version, and I am planning to use pszOS every a few minutes,

Should I use pszOS as a global var or call GetOS() every time?

What's the best option from a performance point of view.

+2  A: 

What's the best option from a performance point of view.

Using a variable is much more efficient than a function call even if the function is empty. Just make sure that you initialize this variable when you have a single thread and then don't change it.

Does it really matter though?

From the info provided it's hard to tell, but is it really important that this runs extremely fast? Do you use it very very very often? It's safer to do the function call especially in a multi threaded environment. Don't optimize prematurely if you aren't sure this is a bottleneck in your program. Code in the safest way possible.

Brian R. Bondy
+2  A: 

you could always just cache the result:

bool GetOS(LPTSTR pszOS)
{
    static bool has_cached = false;
    static bool result = false;
    static TCHAR buf[80];
    if(!has_cached)
    {
        has_cached = true;
        OSVERSIONINFOEX osve;
        BOOL bOsVersionInfoEx;

        ZeroMemory(&osve, sizeof(OSVERSIONINFOEX));

        osve.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);

        if( !(bOsVersionInfoEx = GetVersionEx ((OSVERSIONINFO *) &osve)) )
        {
            result = false;
        }
        else
        {
            StringCchPrintf( buf, 80, TEXT("%u.%u.%u.%u"),
                             osve.dwPlatformId,
                             osve.dwMajorVersion,
                             osve.dwMinorVersion,
                             osve.dwBuildNumber);



            result = true;
        }
    }
    if(result)
        StringCchCat(pszOS, BUFSIZE, buf);
    return result;
}

This way you only calculate everything once, but you keep your logic and initialization all in one place. The static scope of the caching variables also ensure that only this procedure could access the variables so it makes it a little safer than just a regular global variable. I'm only caching the boolean result, if you need to cache more information just add more static variables.

luke
I like that piece of code, I have one question...Should I "unallocate" the memory allocated by ZeroMemory() after the first execution when it finished using it? If so, how?
extintor
Unless im confused about what ZeroMemory does, there is no memory allocation here the osve variable is allocated on the stack and the ZeroMemory call just clears out those bytes on the stack. Cleanup will happen automatically on return from this procedure
luke
Oh ok, im just starting with cpp, thanks a lot.
extintor
@extintor No problem! Figuring out exactly where variables are stored is a major headache in C++ (at least it was for me), so good luck!
luke