Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

union object acts like a structure

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!

like image 257
stanm Avatar asked May 15 '15 14:05

stanm


People also ask

Can we use union in structure?

You use a union when your "thing" can be one of many different things but only one at a time. You use a structure when your "thing" should be a group of other things. In this example, w and g will overlap in memory.

Is structure and union same in C?

A structure is a custom data type that holds multiple members of different data type under a single unit where union is a user defined data type that combine object of different data type in the exact memory location.

What is union in data structure?

A union is a class all of whose data members are mapped to the same address within its object. The size of an object of a union is, therefore, the size of its largest data member. In a structure, all of its data members are stored in contiguous memory locations.

Which of the following is true about structure and union?

True, A structure creates a data type that can be used to group items of possibly different types into a single type. Union is the same as Structures, the union is a user-defined data type. In a union, all members share the same memory location. Hence the correct answer is (i) and (ii) both.


1 Answers

There was a

#define union   struct

in a header file of the project I was debugging.

I am crying.

like image 95
stanm Avatar answered Oct 10 '22 16:10

stanm