Page 1 of 1

CopyStructure question

Posted: Wed Mar 22, 2017 10:57 pm
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)

Re: CopyStructure question

Posted: Wed Mar 22, 2017 11:45 pm
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 ?

Re: CopyStructure question

Posted: Thu Mar 23, 2017 4:01 am
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.

Re: CopyStructure question

Posted: Thu Mar 23, 2017 4:44 pm
by Samuel
Thank you both for the info. :)

Re: CopyStructure question

Posted: Fri Mar 24, 2017 12:46 am
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))

Re: CopyStructure question

Posted: Fri Mar 24, 2017 1:01 am
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.

Re: CopyStructure question

Posted: Fri Mar 24, 2017 4:41 pm
by Samuel
Thanks!