union object acts like a structure

10

UPDATE:

If you want to see the original manifestation of this problem read the "Original question" section.

In a nutshell: I was modifying a field of a C++ union object, but this had no effect on the rest of the fields and it behaved pretty much like a structure. For the solution: see my answer.

Original question

tl;dr: isn't QueryPeformanceCounter supposed to return its value in the QuadPart field of the provided LONG_INTEGER instead of HighPart/LowPart? I couldn't find anywhere that this is system specific, but so it seems to be.

Details

I am getting a peculiar behaviour from Windows's QueryPeformanceCounter. Consider this very simple use, following closely Microsoft's example:

#include <windows.h>

bool test()
{
    LARGE_INTEGER start, end, freq;
    if (!QueryPerformanceFrequency(&freq)) {
        cout << "QueryPerformanceFrequency failed!\n";
        return false;
    }
    QueryPerformanceCounter(&start);

    Sleep(1000);  // Simulate work

    QueryPerformanceCounter(&end);
    cout << "range: from " << start.QuadPart << " to " << end.QuadPart << endl;
    return true;
}

I get the following output:

range: from -3689348814741910324 to -3689348814741910324

This seems quite random, but it is not so. Adding the dumping function:

ostream& operator << (ostream& os, const LARGE_INTEGER& li) {
return os << std::hex << std::setfill('0') << "["
    << "HP: 0x"   << std::setw( 8) << li.HighPart   << ", "
    << "LP: 0x"   << std::setw( 8) << li.LowPart    << ", "
    << "u.HP: 0x" << std::setw( 8) << li.u.HighPart << ", "
    << "u.LP: 0x" << std::setw( 8) << li.u.LowPart  << ", "
    << "QP: 0x"   << std::setw(16) << li.QuadPart   << "]"
    << std::dec << std::setfill(' ');
}

and changing the code to:

bool test()
{
    LARGE_INTEGER start, end, freq;
    cout << "freq:" << endl;
    cout << freq << endl;
    if (!QueryPerformanceFrequency(&freq)) {
        cout << "QueryPerformanceFrequency failed!\n";
        return false;
    }
    cout << freq << endl;

    cout << "start:" << endl;
    cout << start << endl;
    QueryPerformanceCounter(&start);
    cout << start << endl;

    Sleep(1000);  // Simulate work

    cout << "end:" << endl;
    cout << end << endl;
    QueryPerformanceCounter(&end);
    cout << end << endl;

    cout << "range: from " << start.QuadPart << " to " << end.QuadPart << endl;
    return true;
}

yields the following output:

freq:
[HP: 0xcccccccc, LP: 0xcccccccc, u.HP: 0xcccccccc, u.LP: 0xcccccccc, QP: 0xcccccccccccccccc]
[HP: 0x00000000, LP: 0x0025a801, u.HP: 0xcccccccc, u.LP: 0xcccccccc, QP: 0xcccccccccccccccc]
start:
[HP: 0xcccccccc, LP: 0xcccccccc, u.HP: 0xcccccccc, u.LP: 0xcccccccc, QP: 0xcccccccccccccccc]
[HP: 0x0000000a, LP: 0xa6b8ff15, u.HP: 0xcccccccc, u.LP: 0xcccccccc, QP: 0xcccccccccccccccc]
end:
[HP: 0xcccccccc, LP: 0xcccccccc, u.HP: 0xcccccccc, u.LP: 0xcccccccc, QP: 0xcccccccccccccccc]
[HP: 0x0000000a, LP: 0xa6dfb945, u.HP: 0xcccccccc, u.LP: 0xcccccccc, QP: 0xcccccccccccccccc]
range: from -3689348814741910324 to -3689348814741910324

Thus the cryptic value -3689348814741910324 is nothing but the default uninitialized value for QuadPart: 0xcccccccccccccccc. So calling QueryPerformanceCounter(&start); didn't update QuadPart, but LowPart and HighPart instead. From this it is obvious how to extract the actual return value of QueryPerformanceCounter as a LONGLONG:

// Extract the HighPart/LowPart pair from a LARGE_INTEGER as a LONGLONG.
LONGLONG odd_extract(LARGE_INTEGER li) {
    return (static_cast<LONGLONG>(li.HighPart) << 32) + li.LowPart;
}

Now substituting the last output in the test with:

cout << "range: from " << odd_extract(start) << " to " << odd_extract(end) << endl;

outputs

range: from 47158533369 to 47161073403

Finally, computing the elapsed time in seconds returns an expected value:

LONGLONG elapsed = odd_extract(end) - odd_extract(start);
double seconds = static_cast<double>(elapsed) / odd_extract(freq);
cout << "elapsed: " << seconds << " s" << endl;

outputs

elapsed: 1.02861 s

which is to be expected from Windows's inaccurate Sleep().

Now my question is this: isn't QueryPeformanceCounter supposed to return its value in the QuadPart field of the provided LONG_INTEGER instead of HighPart/LowPart? I couldn't find anywhere that this is system specific, but so it seems to be.

UPDATE 1

System: 64-bit Windows 7 Enterprise

Compiler/IDE: MVS 2010 v. 10.0.40219.1 SP1Rel

Definition of _LARGE_INTEGER in C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Include\WinNT.h:

typedef union _LARGE_INTEGER {
    struct {
        DWORD LowPart;
        LONG HighPart;
    } DUMMYSTRUCTNAME;
    struct {
        DWORD LowPart;
        LONG HighPart;
    } u;
#endif //MIDL_PASS
    LONGLONG QuadPart;
} LARGE_INTEGER;

Still, it seems that despite that LARGE_INTEGER is a union it does not behave like one...

UPDATE 2

It seems that I don't see this behaviour with a fresh solution/project. Perhaps there is another problem in the solution that I am trying to measure the performance of that causes this.

In any case, I still have no idea why this happens, so any suggestions on how to resolve it would be welcome, thanks!

c++
windows
performance
winapi
performancecounter
asked on Stack Overflow May 15, 2015 by stanm • edited May 15, 2015 by stanm

1 Answer

35

There was a

#define union   struct

in a header file of the project I was debugging.

I am crying.

answered on Stack Overflow May 15, 2015 by stanm

User contributions licensed under CC BY-SA 3.0