CopyStructure question

Just starting out? Need help? Post your questions and find answers here.
User avatar
Samuel
Enthusiast
Enthusiast
Posts: 755
Joined: Sun Jul 29, 2012 10:33 pm
Location: United States

CopyStructure question

Post by Samuel »

Is it safe to copy a structure that contains an array, but the array is no longer the originally defined size?
My example below works, but I'm unsure if I will ever run into any memory issues.

Code: Select all

EnableExplicit

Structure Vector3
  X.d
  Y.d
  Z.d
EndStructure

Structure BodyData
  Handle.i
  Position.Vector3
  Array Vertex.Vector3(1)
EndStructure
Define.BodyData Body1
Define.BodyData Body2


ReDim Body1\Vertex(2)

Body1\Handle      = 1
Body1\Position\X  = 5.5
Body1\Position\Y  = 2.0
Body1\Position\Z  = 3.1
Body1\Vertex(1)\X = 4.9
Body1\Vertex(1)\Y = 8.0
Body1\Vertex(1)\Z = 3.4
Body1\Vertex(2)\X = 2.2
Body1\Vertex(2)\Y = 2.7
Body1\Vertex(2)\Z = 7.6


CopyStructure(@Body1, @Body2, BodyData)
Debug "Body2's Data"
Debug "Handle = "      + StrD(Body2\Handle)
Debug "Position\X = "  + StrD(Body2\Position\X)
Debug "Position\Y = "  + StrD(Body2\Position\Y)
Debug "Position\Z = "  + StrD(Body2\Position\Z)
Debug "Vertex(1)\X = " + StrD(Body2\Vertex(1)\X)
Debug "Vertex(1)\Y = " + StrD(Body2\Vertex(1)\Y)
Debug "Vertex(1)\Z = " + StrD(Body2\Vertex(1)\Z)
Debug "Vertex(2)\X = " + StrD(Body2\Vertex(2)\X)
Debug "Vertex(2)\Y = " + StrD(Body2\Vertex(2)\Y)
Debug "Vertex(2)\Z = " + StrD(Body2\Vertex(2)\Z)
sys64802
Enthusiast
Enthusiast
Posts: 105
Joined: Sat Sep 12, 2015 6:55 pm

Re: CopyStructure question

Post by sys64802 »

Yes, else it would be a bug.

I also believe and hope a simpler

Body2 = Body1

would work the same way, since the two variables are structured and so pb knows it has to do the same thing as in copystructure, where you have to specify the structured type instead since the two pointers could simply point to some memory buffer (the destination should at least be cleared or containing valid data in some way).
So I hope is not going to make a shallow copy, but I don't why it should do it. Or am I wrong ?
User avatar
Demivec
Addict
Addict
Posts: 4091
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: CopyStructure question

Post by Demivec »

sys64802 wrote:Yes, else it would be a bug.

I also believe and hope a simpler

Body2 = Body1

would work the same way, since the two variables are structured and so pb knows it has to do the same thing as in copystructure, where you have to specify the structured type instead since the two pointers could simply point to some memory buffer (the destination should at least be cleared or containing valid data in some way).
So I hope is not going to make a shallow copy, but I don't why it should do it. Or am I wrong ?
A dynamic array in a structure will be copied correctly. All copies are deep copies.
User avatar
Samuel
Enthusiast
Enthusiast
Posts: 755
Joined: Sun Jul 29, 2012 10:33 pm
Location: United States

Re: CopyStructure question

Post by Samuel »

Thank you both for the info. :)
User avatar
Samuel
Enthusiast
Enthusiast
Posts: 755
Joined: Sun Jul 29, 2012 10:33 pm
Location: United States

Re: CopyStructure question

Post by Samuel »

Sorry, another quick question. Copying a structure containing a dynamic array from within another structure that is also a dynamic array would be safe, correct?
My explanation probably didn't make any since so here's some code that might help explain it a bit better.

Code: Select all

EnableExplicit

Structure Vector3
  Array Link.i(1)
  X.d
  Y.d
  Z.d
EndStructure

Structure BodyData
  Handle.i
  Position.Vector3
  Array Vertex.Vector3(2)
EndStructure
Define.BodyData Body1


ReDim Body1\Vertex(1)\Link(2)

Body1\Vertex(1)\X       = 4.9
Body1\Vertex(1)\Y       = 8.0
Body1\Vertex(1)\Z       = 3.4
Body1\Vertex(1)\Link(1) = 10
Body1\Vertex(1)\Link(2) = 20

;Copy Body1\Vertex(1)'s Vector3 structure into Body1\Vertex(2)'s Vector3 structure
CopyStructure(@Body1\Vertex(1), @Body1\Vertex(2), Vector3)
Debug "Body1\Vertex(2)'s Copied Data"
Debug "Vertex(2)\X = " + StrD(Body1\Vertex(2)\X)
Debug "Vertex(2)\Y = " + StrD(Body1\Vertex(2)\Y)
Debug "Vertex(2)\Z = " + StrD(Body1\Vertex(2)\Z)
Debug "Vertex(2)\Link(1) = " + Str(Body1\Vertex(2)\Link(1))
Debug "Vertex(2)\Link(2) = " + Str(Body1\Vertex(2)\Link(2))
User avatar
Demivec
Addict
Addict
Posts: 4091
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: CopyStructure question

Post by Demivec »

Samuel wrote:Sorry, another quick question. Copying a structure containing a dynamic array from within another structure that is also a dynamic array would be safe, correct?
Yup.
User avatar
Samuel
Enthusiast
Enthusiast
Posts: 755
Joined: Sun Jul 29, 2012 10:33 pm
Location: United States

Re: CopyStructure question

Post by Samuel »

Thanks!
Post Reply