Copying structures

Just starting out? Need help? Post your questions and find answers here.
User_Russian
Addict
Addict
Posts: 1443
Joined: Wed Nov 12, 2008 5:01 pm
Location: Russia

Copying structures

Post by User_Russian »

Suppose we have two structures and one inherits the other. As far as correctly their copy function CopyStructure() and do not call it a problem?

Example.

Code: Select all

Structure Test
  String.s
  List x.s()
EndStructure

Structure TestEx Extends Test
  State.a
  Array y.l(10)
EndStructure

x.Test
y.TestEx

x\String="1234"

For i=0 To 10
  AddElement(x\x())
  x\x() = Str(i)
Next i

CopyStructure(@x, @y, Test)

Debug y\String

ForEach y\x()
  Debug y\x()
Next

Debug y\State

Size = ArraySize(y\y())
Debug Size

For i=0 To Size
  Debug y\y(i)
Next
Code seems to work without errors. But can there be any problems because of this copy?
User avatar
idle
Always Here
Always Here
Posts: 5097
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Copying structures

Post by idle »

It should be fine when using native types and objects
but if you use pointers and heap allocated objects you may need to manually copy nested items.
Windows 11, Manjaro, Raspberry Pi OS
Image
User avatar
skywalk
Addict
Addict
Posts: 4003
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Copying structures

Post by skywalk »

Apologize if these have been asked before?
What is the difference between:
CopyStructure(Struct1, Struct2, someStruct)
and
Struct2.someStruct = Struct1.someStruct?

ClearStructure(@Struct1, someStruct) vs ResetStructure(@Struct1, someStruct)?
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
#NULL
Addict
Addict
Posts: 1440
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: Copying structures

Post by #NULL »

i think CopyStructure() makes sense only if Struct2 was a pointer (*Struct2) where assignment wouldn't copy.

ClearStructure() will invalidate/delete any List or Map object ect. contained in the structure and would need an additional InitializeStructure() to make those usable again.

ResetStructure() will empty those but keep them alive.
User avatar
skywalk
Addict
Addict
Posts: 4003
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Copying structures

Post by skywalk »

Thanks, I'll reference your reply in my comments for Structures.
I read the help a few times without seeing the differences.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Post Reply