Qbasicnews.com

Full Version: Unions, Fields, et cetera.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Alright. I've got a very, very rough idea about what a Union is, but I'm not too sure. (I'm not the best C'er.) Can somebody explain it to me?

And Fields... when v1ctor gave me some insight as to how to fix my problem with a data structure called WIN32_FIND_DATA, he told me to change the type declarator to "Type WIN32_FIND_DATA Field = 1" but I'm not sure what this really did. Can somebody help me out, here, too?

Just as well, I'd like ask, "what would be the advantage of adding a typedef-like statement to FreeBASIC?" We've already got TYPE, and we've got #DEFINE that'll replace anything with anything else, which could technically be used as a typedef would be. Unless there's something I'm missing or just plain not thinking about...
Quote:Alright. I've got a very, very rough idea about what a Union is, but I'm not too sure. (I'm not the best C'er.) Can somebody explain it to me?
Unions share the same memory. All members in a union
access the same memory:
Code:
Type xyz
  Union
    a as Integer
    b as Integer
  End Union
End Type

Dim x As xyz

x.a = 12
Print x.b
a and b share its memory here - you access the same
memory with a and b.

You can use it also to store different types:
Code:
Type xyz
  Union
    a as Integer
    b as Single
  End Union
End Type

Dim x As xyz

x.b = 1.2345
Print x.a

The sizeof(xyz) in the above examples is 4 Bytes, because
Integer and Single are both 4Bytes in size.

The union is as big as the biggest member in it:
Code:
Type xyz
  Union
    a as Integer
    b as Double
  End Union
End Type

Dim x As xyz

x.b = 1.2345
Print x.a
Now the sizeof(xyz) is 8 Bytes, because the biggest
member is Double, which uses 8 Bytes of memory.


Quote:And Fields... when v1ctor gave me some insight as to how to fix my problem with a data structure called WIN32_FIND_DATA, he told me to change the type declarator to "Type WIN32_FIND_DATA Field = 1" but I'm not sure what this really did. Can somebody help me out, here, too?
It looks like FIELD is used for alignment here.

Lets say you have this structure:
Code:
Type xyz
  a as Byte
  b as Integer
  c as Byte
  d as Integer
End Type
Compilers can optimize this structure and align the 2 Integers
on DWORD-Boundaries - because access to aligned Integers
is faster in the processor.

The compiler would optimize the above structure internally to:
Code:
Type xyz

  a as Byte
  0
  0
  0

  b as Integer

  c as Byte
  0
  0
  0

  d as Integer
End Type
It inserts NULL-Fields, so the Integers are aligned on a DWORD-Boundary.

If you set FIELD=1, it takes the structure/type as it is, without
aligning it - without inserting the extra space.

For API-Types its needed to use FIELD=1, so Types are
exactly like you write it in the source.


I´m not a QB/VB-Coder and i am new here, but should be correct -
because its all logical. Wink
I couldn't have explained it better ;)
Does alligning the type have any benefits speedwise at program (and not compiler) runtime?
A bit on modern CPUs, but that's not the problem, all 32-bit C compilers do that default alignament, so, not having that would make porting of C-only APIs/headers near to impossible to do - SDL for example.

Just now i got the field alignament working *exactly* as in C (code is not even at CVS yet), took me hours of hacking to find out what GCC was doing.. anywayz..
Quote: took me hours of hacking to find out what GCC was doing..
Whatever you want him to do:
Code:
__attribute__((aligned(...)))
__attribute__((packed))

Wink
Heh, do you really think i don't know how to tell gcc to pack or not structs? C'mon, that's not what i meant.. nm..
There is a Wink at the end of my posting. Of course it's non-sense.