Swap structure addresses?
-
- User
- Posts: 67
- Joined: Mon Jan 16, 2006 10:12 pm
Swap structure addresses?
Is it possible to swap the addresses of structures and not just their fields if they are of the same type?
I searched and found some possible methods, but nothing specific.
Cheers, Jon
I searched and found some possible methods, but nothing specific.
Cheers, Jon
something like this ?
Code: Select all
Procedure SwapStruct(*a, *b, nByte.l)
Protected *temp = AllocateMemory(nByte)
If *temp
CopyMemory(*a, *temp, nByte)
CopyMemory(*b, *a, nByte)
CopyMemory(*temp, *b, nByte)
EndIf
EndProcedure
Structure TEST
id.s
name.s
EndStructure
a.TEST
a\id = "0123"
a\name = "donald"
b.TEST
b\id = "4567"
b\name = "mickey"
Debug a\id
Debug a\name
Debug b\id
Debug b\name
SwapStruct(a, b, SizeOf(TEST))
Debug a\id
Debug a\name
Debug b\id
Debug b\name
No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
hmmm... interesting:
Code: Select all
Structure TEST
id.s
name.s
EndStructure
a.TEST
a\id = "1"
a\name = "donald"
*a.TEST = @a
b.TEST
b\id = "2"
b\name = "mickey"
*b.TEST = @b
Debug *a\id + " : " + *a\name
Debug *b\id + " : " + *b\name
Debug ""
Swap *a, *b
Debug *a\id + " : " + *a\name
Debug *b\id + " : " + *b\name
Debug ""
Swap *a\id, *b\id
Debug *a\id + " : " + *a\name
Debug *b\id + " : " + *b\name
Debug ""
Swap *a\name, *b\name
Debug *a\id + " : " + *a\name
Debug *b\id + " : " + *b\name
Debug ""
- netmaestro
- PureBasic Bullfrog
- Posts: 8451
- Joined: Wed Jul 06, 2005 5:42 am
- Location: Fort Nelson, BC, Canada
I'm making fairly heavy use of this concept here:
http://www.purebasic.fr/english/viewtopic.php?t=21245
http://www.purebasic.fr/english/viewtopic.php?t=21245
BERESHEIT
-
- User
- Posts: 67
- Joined: Mon Jan 16, 2006 10:12 pm
Haven't tried this stuff yet, but I'm figuring that since a structure is an array of pointers (correct?) it should work if you just swapped out the addresses as in:
@TempStructA(0) = @TempStructB(0)
Of course, that doesn't actually work but it's the concept I had in mind. I can just implement a procedure that swaps the elements with the Swap command, but this would be a lot quicker and easier.
Thanks all.
Cheers, Jon
@TempStructA(0) = @TempStructB(0)
Of course, that doesn't actually work but it's the concept I had in mind. I can just implement a procedure that swaps the elements with the Swap command, but this would be a lot quicker and easier.
Thanks all.
Cheers, Jon
-
- User
- Posts: 67
- Joined: Mon Jan 16, 2006 10:12 pm
Code: Select all
; This works ONLY when you use a pointer to a structure
*Obnoxious.POINT = AllocateMemory(SizeOf(POINT))
*Nox.POINT = AllocateMemory(SizeOf(POINT))
*Obnoxious\x = 1
*Obnoxious\y = 2
*Nox\x = 3
*Nox\y = 4
Swap *Obnoxious, *Nox
Debug *Obnoxious\x
Debug *Obnoxious\y
Debug *Nox\x
Debug *Nox\y

-
- User
- Posts: 67
- Joined: Mon Jan 16, 2006 10:12 pm
-
- User
- Posts: 67
- Joined: Mon Jan 16, 2006 10:12 pm
I noticed the memory leak and added the FreeMemory.
However, I was just using a straight CopyMemory command as in CopyMemory(@StructureA, @StructureB, SizeOf(Structure)). Worked like a charm until reDIMing to clear the array. Unfortunately, I've gone back to longhand and I never saved a version that used the redim problem. I'll try it in a new file and post it.
Cheers, Jon
However, I was just using a straight CopyMemory command as in CopyMemory(@StructureA, @StructureB, SizeOf(Structure)). Worked like a charm until reDIMing to clear the array. Unfortunately, I've gone back to longhand and I never saved a version that used the redim problem. I'll try it in a new file and post it.
Code: Select all
Procedure SwapStructure(*StructureAddressA, *StructureAddressB, SizeOfStructureInBytes.l)
Protected *CopyBuffer = AllocateMemory(SizeOfStructureInBytes)
If *CopyBuffer
CopyMemory(*StructureAddressA, *CopyBuffer, SizeOfStructureInBytes)
CopyMemory(*StructureAddressB, *StructureAddressA, SizeOfStructureInBytes)
CopyMemory(*CopyBuffer, *StructureAddressB, SizeOfStructureInBytes)
FreeMemory(*CopyBuffer)
EndIf
EndProcedure
Code: Select all
-
- User
- Posts: 67
- Joined: Mon Jan 16, 2006 10:12 pm
Code: Select all
Structure Test
a.l
b.l
c.l
d.s
EndStructure
Global Dim Testa.Test(10)
Global Dim Testb.Test(10)
Testa(1)\a = 10
Testa(1)\d = "Varmint"
CopyMemory(@Testa(1), @Testb(1), SizeOf(Test))
Debug Testb(1)\a
Debug Testb(1)\d
Global Dim Testa.Test(10)
Global Dim Testb.Test(10)
Jon

-
- User
- Posts: 67
- Joined: Mon Jan 16, 2006 10:12 pm
Well, I must've been doing something wrong, because swapping the addresses of the structures now works like a charm. I'm being careful to make sure everything is swapped back before I DIM again, but it doesn't seem as if I have to. Had to go to this method because I was swapping a lot of variables a number of times during a redraw. (Painting two sets of musical structures, question/answer, on the same staff, piano, and guitar.)
Cheers, Jon

Cheers, Jon

