6

I have found this function GetFileSizeEx(), which returns the size of file in PLARGE_INTEGER that is formed by the union of structures.

typedef union _LARGE_INTEGER {
  struct {
    DWORD LowPart;
    LONG  HighPart;
  } ;
  struct {
    DWORD LowPart;
    LONG  HighPart;
  } u;
  LONGLONG QuadPart;
} LARGE_INTEGER, *PLARGE_INTEGER;

Is it same as if I'd call it structure of structures? How can I figure the file size that it has returned and how large information can it handle?

Jason
  • 115
  • 1
  • 4

2 Answers2

5

You are probably misunderstanding what a union is. A file's length is obtained by

LARGE_INTEGER  len_li;
GetFileSizeEx (hFile, &len_li);
int64 len = (len_li.u.HighPart << 32) | len_li.u.LowPart;

Alternatively, you can access the 64 bit representation directly with modern compilers:

LARGE_INTEGER  len_li;
GetFileSizeEx (hFile, &len_li);
LONGLONG  len_ll = len_li.QuadPart;
wallyk
  • 56,922
  • 16
  • 83
  • 148
  • Somehow this feels like undefined behaviour. It is only valid to read a union through the member that it was last written to through. How do you know that `QuadPart` is that last member? – Kerrek SB Aug 29 '11 at 00:19
  • @Kerrek, in this case, the purpose of the union is to interpret the same data in different ways. So it is legitimate to write to one union member and read from another. – Branko Dimitrijevic Aug 29 '11 at 00:22
  • @Branko: How does purpose legal code make? If the purpose of my double free is to make sure a variable is really gone, does that make it OK? The only out-of-order union access that is defined is if one of the members is a char-array, I believe. – Kerrek SB Aug 29 '11 at 00:24
  • @Kerrek SB: This particular union provides multiple methods for accessing the exact same data layout, so there is no problem with accessing any particular member. What you are alluding to is considered union (ab)use where the data type stored in the union varies based on context elsewhere. For example, a script interpreter might have a union which hold the current `string`, `int`, or `float` value based on another field which gives the current data type. – wallyk Aug 29 '11 at 00:25
  • @Kerrek: Yes, it is technically undefined behavior. But technicalities don't matter much when everyone does this kind of stuff with unions. It may not be defined, but it is _reliable_ across compilers. – Nicol Bolas Aug 29 '11 at 00:34
  • 2
    @KerrekSB: It works, because the company that created this `union` also created a compiler that compiles this `union` and defined the required behavior. I.e. its functionality was not defined by a group of cooperating compiler builders (ISO WG21) but by a single one. – MSalters Aug 29 '11 at 07:58
  • I first read " when everyone does this kind of stuff with unicorns" @NicolBolas... :-D – Prof. Falken Jul 25 '12 at 23:44
1

no a union is NOT a struct of structs.

I suggest you read this question and answers: Difference between a Structure and a Union in C

hope this helps to clarify :)

Community
  • 1
  • 1
Davide Piras
  • 43,984
  • 10
  • 98
  • 147