By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,120 Members | 1,799 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,120 IT Pros & Developers. It's quick & easy.

Why does Platform SDK define union LARGE_INTEGER in such a way?

P: n/a
Why does Platform SDK define union LARGE_INTEGER in such a way?

ntdef.h defines the union LARGE_INTEGER as follows:
typedef union _LARGE_INTEGER
{
struct
{
ULONG LowPart;
LONG HighPart;
};
struct
{
ULONG LowPart;
LONG HighPart;
} u;
LONGLONG QuadPart;

} LARGE_INTEGER;
Though I fully understand what union is, I cannot think out the reason
why it adds struct u into the union. In fact, the struct u is
equivalent to the unnamed struct, so we can alway use the unnamed
struct instead of struct u. To my thinking, the union should be
designed as follows:

typedef union _LARGE_INTEGER
{
struct
{
ULONG LowPart;
LONG HighPart;
};
LONGLONG QuadPart;

} LARGE_INTEGER;
Am I right?

Who can tell me what the consideration for the former definition is,
and what does it mean by the struct name "u" (means "unsigned" ?, I'm
not sure.)?
Thanks in advance.

Oct 6 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a

Lighter wrote:
Why does Platform SDK define union LARGE_INTEGER in such a way?

ntdef.h defines the union LARGE_INTEGER as follows:
typedef union _LARGE_INTEGER
{
struct
{
ULONG LowPart;
LONG HighPart;
};
struct
{
ULONG LowPart;
LONG HighPart;
} u;
LONGLONG QuadPart;

} LARGE_INTEGER;
Though I fully understand what union is, I cannot think out the reason
why it adds struct u into the union. In fact, the struct u is
equivalent to the unnamed struct, so we can alway use the unnamed
struct instead of struct u. To my thinking, the union should be
designed as follows:

typedef union _LARGE_INTEGER
{
struct
{
ULONG LowPart;
LONG HighPart;
};
LONGLONG QuadPart;

} LARGE_INTEGER;
Am I right?

Who can tell me what the consideration for the former definition is,
and what does it mean by the struct name "u" (means "unsigned" ?, I'm
not sure.)?
Thanks in advance.
Oh, another guy works in Windows. See the following link:

http://msdn.microsoft.com/library/de...nteger_str.asp

.. Ask a programmer who works for Microsoft. Maybe they think of
compatibility.

Oct 6 '06 #2

P: n/a
Lighter wrote:
Why does Platform SDK define union LARGE_INTEGER in such a way?

ntdef.h defines the union LARGE_INTEGER as follows:
typedef union _LARGE_INTEGER
The tag name is reserved for implementation use. I think you're showing
a file from your implementation, in which case it is free to use these
reserved names, as well as non-standard features that may be provided.
{
struct
{
ULONG LowPart;
LONG HighPart;
};
Unnamed union members are not available in standard C.
>
struct
{
ULONG LowPart;
LONG HighPart;
} u;
LONGLONG QuadPart;

} LARGE_INTEGER;
Though I fully understand what union is, I cannot think out the reason
why it adds struct u into the union. In fact, the struct u is
equivalent to the unnamed struct, so we can alway use the unnamed
struct instead of struct u. To my thinking, the union should be
designed as follows:
No, the unnamed struct is illegal in standard C. If you want to write
standard C, leave out the unnamed struct and keep u. If you want to
write non-standard C, move to a platform-specific newsgroup.
typedef union _LARGE_INTEGER
{
struct
{
ULONG LowPart;
LONG HighPart;
};
LONGLONG QuadPart;

} LARGE_INTEGER;
Am I right?
No, this suffers from the same problem. You removed the wrong struct.
Who can tell me what the consideration for the former definition is,
and what does it mean by the struct name "u" (means "unsigned" ?, I'm
not sure.)?
I suppose, on compilers that allow unnamed struct members of unions, it
gives the user an option whether to write

LARGE_INTEGER i; i->u->LowPart = 0xFFFFFFFF;

or

LARGE_INTEGER i; i->LowPart = 0xFFFFFFFF;

--
Simon.
Oct 6 '06 #3

P: n/a
"Lighter" <cq****@gmail.comwrites:
Why does Platform SDK define union LARGE_INTEGER in such a way?

ntdef.h defines the union LARGE_INTEGER as follows:
typedef union _LARGE_INTEGER
{
struct
{
ULONG LowPart;
LONG HighPart;
};
struct
{
ULONG LowPart;
LONG HighPart;
} u;
LONGLONG QuadPart;

} LARGE_INTEGER;
Because the author(s) of that code don't care about portability. The
identifer _LARGE_INTEGER is reserved to the implementation (perhaps
the Platform SDK is part of the implementation?). Anonymous members
are not legal in standard C. The code assumes that the low-order half
is stored at the lowest address.

The typedefs (or are they macros?) ULONG, LONG, and LONGLONG are,
IMHO, horribly bad style. If they're always aliases for unsigned
long, long, and long long respectively, then the code should use
unsigned long, long, and long long. If they might sometimes refer to
other types, then the names ULONG, LONG, and LONGLONG are misleading.

Leaving that aside, I suppose the idea is that you can declare

LARGE_INTEGER x;

and then refer to the numeric value as x.QuadPart, and to the
low-order half as either x.LowPart (non-portably) or x.u.LowPart
(portably, if you drop the anonymous structure from the union
declaration), and likewise for the high-order half.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Oct 6 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Browse more C / C++ Questions on Bytes