Code: Select all
Structure Type
Name$
StructureUnion
Long.l ; Each field (Long, Float and String) resides at the
Float.f ; place in memory.
String.s ;
EndStructureUnion
EndStructure- np

Code: Select all
Structure Type
Name$
StructureUnion
Long.l ; Each field (Long, Float and String) resides at the
Float.f ; place in memory.
String.s ;
EndStructureUnion
EndStructureCode: Select all
Structure Combined
StructureUnion
x.l
PosX.l
dx.l
EndStructureUnion
StructureUnion
y.l
posy.l
dy.l
EndStructureUnion
EndStructure
Typ.Combined
Typ\x=10
Typ\y=20
Debug Typ\dx
Debug Typ\PosX
Debug Typ\dy
Debug Typ\posy
All the variables inside a union are located at the same address in memory. The size of the union (in bytes taken up by it) is the size of the largest item inside the union.NoahPhense wrote:I'm trying to get a grasp on the above.. any information will help.. thx
Code: Select all
DefType.Type foo
foo\String = "blah"
Debug foo\Long
Debug @foo\String
Code: Select all
Structure myfloat
StructureUnion
fnumber.f
fbyte.b[4]
EndStructureUnion
EndStructure
x.myfloat
x\fnumber=3.141593
Code: Select all
print(hex(x\fbyte[1])) ;fbyte[1] through fbyte[4]

Excellent help guys.. thanks.. as for this example, it came from the PBGPI wrote:With StructureUnion you can create "Aliases"
StructureUnion with String and other Variable-Typ combined isn't a good idea (my opinion)).
Code: Select all
Structure Type
Name$
StructureUnion
Long.l ; Each field (Long, Float and String) resides at the
Float.f ; place in memory.
String.s ;
EndStructureUnion
EndStructure
DefType.Type foo
foo\Name$ = "PB"
Debug foo\Name$
foo\String = "blah"
Debug foo\String ; the string
Debug @foo\String ; the address
Debug PeekS(@foo\String) ; pull string from address
foo\Float = 1.234
Debug foo\Float
foo\Long = 8765
Debug foo\Long
; So as you've stated, they are kind of like alias's. They can be
; utilized how ever you wish, as long as you have defined them
; first with StructureUnion.
;
; Is the main advantage of this, to save on memory(size)?Code: Select all
Structure IPType
Reserved.w
Port.w
StructureUnion
IPLong.l
IP.b[4]
EndStructureUnion
Zeros.l[2]
EndStructure
length.l = SizeOf(IPType)
result.l = getpeername_(cnid, @IP.IPType, @length)
If result = 0
remoteip$ = StrU(IP\IP[0],#Byte)+"."+StrU(IP\IP[1], #Byte)+"."
;remoteip$ + StrU(IP\IP[2],#Byte)+"."+StrU(IP\IP[3], #Byte)+":"+StrU(IP\Port,#Word) ;remote port
remoteip$ + StrU(IP\IP[2],#Byte)+"."+StrU(IP\IP[3], #Byte); without remote port
Else
result = WSAGetLastError_()
EndIf
result.l = getsockname_(cnid, @IP.IPType, @length)
If result=0
localip$ = StrU(IP\IP[0],#Byte)+"."+StrU(IP\IP[1], #Byte)+"."
;localip$ + StrU(IP\IP[2],#Byte)+"."+StrU(IP\IP[3], #Byte)+":"+StrU(IP\Port,#Word) ;local port
localip$ + StrU(IP\IP[2],#Byte)+"."+StrU(IP\IP[3], #Byte) ; without local port
Else
result = WSAGetLastError_()
EndIfCode: Select all
result.l = getpeername_(cnid, @IP.IPType, @length)Code: Select all
Structure myNewType
a.s
b.s
c.s
StructureUnion
There.l
Can.w
Be.s
Only.b
One.f
EndStructureUnion
EndStructure
Sector1.myNewType
Sector1\a = "I love it when"
Debug Sector1\a
Sector1\b = "I learn new ways"
Debug Sector1\b
Sector1\c = "to handle data."
Debug Sector1\c
Debug ""
Sector1\There = 11
Debug Sector1\There
Sector1\Can = -30000
Debug Sector1\Can
Sector1\Be = "not recommended" ; @PB: could you tell me more/why not recommended?
Debug Sector1\Be
Sector1\Only = 127
Debug Sector1\Only
Sector1\One = 22.33
Debug Sector1\One
Code: Select all
Structure Person
Name.s ; string = 4
ForName.s ; string = 4
Age.w ; word = 2
EndStructure
a.Person
Debug SizeOf(a) ; 10
Depends how you view it. Obviously, because the fields all occupy the same address in memory you cannot actually have them all storing different things at the same time. Your "1.234" will overwrite the "blah" string, and the "8765" will overwrite the "1.234".NoahPhense wrote:; Is the main advantage of this, to save on memory(size)?[/code]
Code: Select all
Structure variant
type.w
structureunion
byte.b
word.w
; etc
endstructureunion
endstructure
Hmm, i'm sure there should be a proper type for that. sockaddr_in IIRC. Why do people re-invent the wheel so often? Anyhoo, I digress :)NoahPhense wrote:Code: Select all
Structure IPType Reserved.w Port.w StructureUnion IPLong.l IP.b[4] EndStructureUnion Zeros.l[2] EndStructure
Yes, the compiler will create the variable wherever you first use it, regardless of what you are doing to it at the time.NoahPhense wrote: In this line:Where he writes @IP.IPType is this where he is creating IP?Code: Select all
result.l = getpeername_(cnid, @IP.IPType, @length)
Instead of creating it first and then pointing to it like such?
IP.IPType or DefType.IPType IP
result.l = getpeername_(cnid, @IP.IPType, @length)
I just wasn't aware that you could create it and point to it in the same
command set. But its functional so I guess that's a yes.
NoahPhense wrote: -- and lastly, just to be sure that I understand StructureUnions
NoahPhense wrote:** Ok, how about SizeOf() .. ?
Is that the reason it is equal to 10 4+4+2, and they are bytes, right?Code: Select all
Structure Person Name.s ; string = 4 ForName.s ; string = 4 Age.w ; word = 2 EndStructure a.Person Debug SizeOf(a) ; 10
So that structure only takes 10 bytes to (create) ?
How can you find out the size of your structures with data in them?
- np

Could you show me a full example of how you would use a variant?tinman wrote:..other people may want to use it as a generic type and be able to use it as any type of variable.
tinman wrote:Hmm, i'm sure there should be a proper type for that. sockaddr_in IIRC. Why do people re-invent the wheel so often? Anyhoo, I digress

Not really since it's not something I've looked into as I have no need for it. I know other people here have done it though.NoahPhense wrote:Could you show me a full example of how you would use a variant?tinman wrote:..other people may want to use it as a generic type and be able to use it as any type of variable.
