Page 1 of 1

Copy Structure

Posted: Mon Nov 11, 2024 8:30 pm
by mestnyi
I would like to know what is the difference between these three copying methods?

Code: Select all

Structure People
    Name$
    LastName$
    Map Friends$()
    Age.l
  EndStructure
  
  Student.People
  Student\Name$ = "Paul"
  Student\LastName$ = "Morito"
  Student\Friends$("Tom") = "Jones"
  Student\Friends$("Jim") = "Doe"
  
  ;\\ 1
  *StudentCopy.People = Student
  
  ;\\ 2
  ;   *StudentCopy.People = @Student
  
  ;\\ 3
  ;   *StudentCopy.People = AllocateStructure(People)
  ;    CopyStructure(@Student, *StudentCopy.People, People)
  
  
  Debug *StudentCopy\Name$
  Debug *StudentCopy\LastName$
  Debug *StudentCopy\Friends$("Tom")
  Debug *StudentCopy\Friends$("Jim")

Re: Copy Structure

Posted: Mon Nov 11, 2024 8:43 pm
by Demivec
Copy method 4 copies the structure and when that copy (i.e. the structured variable) goes out of scope it will be automatically freed.

Copy method 3 copies the structure but the copy is going to go out of scope it likewise needs to have a FreeStructure() called with the address in the structured pointer.


Copy method 1 & 2 is not a copy of the structure. If it works it will end up simply pointing at the structured variable memory of the source that was supposed to instead be copied but wasn't.

@Edit: I missed that method 1 also used a pointer and instead had thought it was the same as method which wasn't posted until later.

Re: Copy Structure

Posted: Tue Nov 12, 2024 10:46 am
by NicTheQuick
Number 1 and 2 are essential the same. It only creates a reference to the Variable `Student`.
Number 3 really makes a copy of the whole structure.

But you also forgot this method:

Code: Select all

Structure People
	Name$
	LastName$
	Map Friends$()
	Age.l
EndStructure

Define Student.People
Student\Name$ = "Paul"
Student\LastName$ = "Morito"
Student\Friends$("Tom") = "Jones"
Student\Friends$("Jim") = "Doe"

Define *StudentCopy.People
;\\ 1
; *StudentCopy = Student

;\\ 2
; *StudentCopy = @Student

;\\ 3
; *StudentCopy = AllocateStructure(People)
; CopyStructure(@Student, *StudentCopy.People, People)

; Debug *StudentCopy\Name$
; Debug *StudentCopy\LastName$
; Debug *StudentCopy\Friends$("Tom")
; Debug *StudentCopy\Friends$("Jim")

;\\ 4
Define StudentCopy.People
StudentCopy = Student

Debug StudentCopy\Name$
Debug StudentCopy\LastName$
Debug StudentCopy\Friends$("Tom")
Debug StudentCopy\Friends$("Jim")

Re: Copy Structure

Posted: Mon May 12, 2025 5:40 pm
by Piero
Number 3 gives error, and help is not very clear…

Is this right (including Allocate to "clean" memory)?

Code: Select all

*StudentCopy = AllocateStructure(People)
CopyStructure(@Student, @StudentCopy.People, People)
Thanks in advance

Re: Copy Structure

Posted: Mon May 12, 2025 5:52 pm
by NicTheQuick
Piero wrote: Mon May 12, 2025 5:40 pm Number 3 gives error, and help is not very clear…

Is this right (including Allocate to "clean" memory)?

Code: Select all

*StudentCopy = AllocateStructure(People)
CopyStructure(@Student, @StudentCopy.People, People)
Thanks in advance
No. That's wrong. If you would use `EnableExplicit` you would have get an error message because `*StudentCopy` is not the same variable as `StudentCopy`.
So either you do it like this:

Code: Select all

EnableExplicit
Structure People
	Name$
	LastName$
	Map Friends$()
	Age.l
EndStructure
Define Student.People
Define *StudentCopy.People = AllocateStructure(People)
CopyStructure(@Student, *StudentCopy, People)
Or like this:

Code: Select all

EnableExplicit
Structure People
	Name$
	LastName$
	Map Friends$()
	Age.l
EndStructure
Define Student.People
Define StudentCopy.People
CopyStructure(@Student, @StudentCopy, People)

Re: Copy Structure

Posted: Mon May 12, 2025 6:46 pm
by Piero
Thanks!
Forgive my noob confusion about "@variable address" and "*address" :oops:

PS: I think AllocateStructure should be mentioned on CopyStructure help (it's very unclear IMHO… anyway this is "advanced" stuff…)

Re: Copy Structure

Posted: Tue May 13, 2025 10:30 am
by NicTheQuick
Piero wrote: Mon May 12, 2025 6:46 pm Thanks!
Forgive my noob confusion about "@variable address" and "*address" :oops:

PS: I think AllocateStructure should be mentioned on CopyStructure help (it's very unclear IMHO… anyway this is "advanced" stuff…)
You don't have to apologize. It's normal if you don't understand straight away. But I like to write examples because I can learn well from examples myself. I hope it helps.

Re: Copy Structure

Posted: Tue May 13, 2025 1:35 pm
by Piero
NicTheQuick wrote: Tue May 13, 2025 10:30 amYou don't have to apologize. It's normal if you don't understand straight away. But I like to write examples because I can learn well from examples myself. I hope it helps.
Why not? * and @ are """basic PB stuff""" :oops:

I still didn't really need it, but I was "investigating" PB options for "ultra-fast operations using pointers"
I'm more into """"high-level"""" programming since a long time; I'm "rusty" about that stuff…
So, from my point of view, I must repeat: help was a bit confusing…