How to copy a structure

Share your advanced PureBasic knowledge/code with the community.
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

How to copy a structure

Post by Mistrel »

This is pretty simple but still pertinent to Tricks 'n' Tips.

Code: Select all

Define box1.RECT

box1\left=1
box1\top=2
box1\right=3
box1\bottom=4

mem=AllocateMemory(SizeOf(RECT))

CopyMemory(@box1.RECT,mem,SizeOf(RECT))

*box2.RECT=mem

Debug *box2\left
Debug *box2\top
Debug *box2\right
Debug *box2\bottom
Fred
Administrator
Administrator
Posts: 18252
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

Just ensure there is no string in your structure, or you will run into problems.
dige
Addict
Addict
Posts: 1412
Joined: Wed Apr 30, 2003 8:15 am
Location: Germany
Contact:

Post by dige »

please note that works not with included strings...
dige
Addict
Addict
Posts: 1412
Joined: Wed Apr 30, 2003 8:15 am
Location: Germany
Contact:

Post by dige »

ha! Fred was faster :D
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Post by Mistrel »

You can with fixed strings. Thanks, Fred.

Code: Select all

Structure ARECT
	left.l
	top.l
	right.l
	bottom.l
	string.s{6}
EndStructure

Define box1.ARECT

box1\left=1
box1\top=2
box1\right=3
box1\bottom=4
box1\string="Hello!"

mem=AllocateMemory(SizeOf(ARECT))

CopyMemory(@box1.ARECT,mem,SizeOf(ARECT))

*box2.ARECT=mem

Debug *box2\left
Debug *box2\top
Debug *box2\right
Debug *box2\bottom
Debug *box2\string
User avatar
Joakim Christiansen
Addict
Addict
Posts: 2452
Joined: Wed Dec 22, 2004 4:12 pm
Location: Norway
Contact:

Post by Joakim Christiansen »

Fred wrote:Just ensure there is no string in your structure, or you will run into problems.
That's why we need you to make a function that works with strings too ;)
I like logic, hence I dislike humans but love computers.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Joakim Christiansen wrote:
Fred wrote:Just ensure there is no string in your structure, or you will run into problems.
That's why we need you to make a function that works with strings too ;)
Copying a structure containing strings to one created dynamically with AllocateMemory() requires that you take steps to free the copies when the memory is released as Purebasic will not do this for you. Still, it's easy enough. :)
I may look like a mule, but I'm not a complete ass.
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Post by Mistrel »

If we had TypeOf() we could make a macro for this.

But from what I understand this isn't possible with PureBasic's type system.
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

Fred wrote:Just ensure there is no string in your structure, or you will run into problems.
Should not be:
Just ensure there is no string in your structure, or you could run into problems.
:?:
Because this works as expected without errors :? :?:

Code: Select all

Structure ARECT 
   left.l 
   top.l 
   right.l 
   bottom.l 
   string.s{6}
   another$
   another.s
   anotherone$
EndStructure 

Define box1.ARECT 

box1\left=1 
box1\top=2 
box1\right=3 
box1\bottom=4 
box1\string="Hello!"
box1\another$="Hellooo!"
box1\another="Helloooooooo!.s"
box1\anotherone$="Hiiiiiiiiooooooo!"

*box2.ARECT=AllocateMemory(SizeOf(ARECT)) 

CopyMemory(@box1.ARECT,*box2.ARECT,SizeOf(ARECT)) 

Debug *box2\left 
Debug *box2\top 
Debug *box2\right 
Debug *box2\bottom 
Debug *box2\string
Debug *box2\another$
Debug *box2\another
Debug *box2\anotherone$
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

That is what I would call a 'shallow copy' of the strings as only the underlying pointers are copied and can lead to all kinds of problems.

E.g. altering one of the string fields in box1 could seriously invalidate the fields of box2. The following (on my system at least) shows the problem in that the 'another$' field of box2 is corrupted after altering the corresponding field in box1.

Code: Select all

Structure ARECT 
   left.l 
   top.l 
   right.l 
   bottom.l 
   string.s{6} 
   another$ 
   another.s 
   anotherone$ 
EndStructure 

Define box1.ARECT 

box1\left=1 
box1\top=2 
box1\right=3 
box1\bottom=4 
box1\string="Hello!" 
box1\another$="Hellooo!" 
box1\another="Helloooooooo!.s" 
box1\anotherone$="Hiiiiiiiiooooooo!" 

*box2.ARECT=AllocateMemory(SizeOf(ARECT)) 

CopyMemory(@box1.ARECT,*box2.ARECT,SizeOf(ARECT)) 

box1\another$="OYdddddddddddddddddddddddddddddddddddddddddddddddddd!"

Debug *box2\another$ 
The problem here is that the original string pointer copied to box2 has been invalidated by the line :

Code: Select all

box1\another$="OYdddddddddddddddddddddddddddddddddddddddddddddddddd!"
In this respect, I would heed Fred's original words carefully! :wink:
I may look like a mule, but I'm not a complete ass.
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

Aha!, you refreshed my mem :)
Then there is no problem if the strings are not touched? :?
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Yes, but imagine, for example, that the original structure is local to a procedure, then on exit from the procedure all string fields copied to dynamic memory will be instantly invalidated!

A risky business and not one to be found in 'srod's good guide to programming!' :)
I may look like a mule, but I'm not a complete ass.
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

Yes, then Fred was right when said "will" :wink:
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
Ollivier
Enthusiast
Enthusiast
Posts: 281
Joined: Mon Jul 23, 2007 8:30 pm
Location: FR

Post by Ollivier »

In order to copy a structure, there's a solution but you must notify the structure to the procedure via a specific string.

http://www.purebasic.fr/english/viewtop ... highlight=
Post Reply