Copy Structure

Just starting out? Need help? Post your questions and find answers here.
mestnyi
Addict
Addict
Posts: 1102
Joined: Mon Nov 25, 2013 6:41 am

Copy Structure

Post 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")
User avatar
Demivec
Addict
Addict
Posts: 4281
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Copy Structure

Post 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.
Last edited by Demivec on Tue Nov 12, 2024 10:54 am, edited 1 time in total.
User avatar
NicTheQuick
Addict
Addict
Posts: 1527
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: Copy Structure

Post 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")
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
User avatar
Piero
Addict
Addict
Posts: 1040
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Re: Copy Structure

Post 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
User avatar
NicTheQuick
Addict
Addict
Posts: 1527
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: Copy Structure

Post 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)
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
User avatar
Piero
Addict
Addict
Posts: 1040
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Re: Copy Structure

Post 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…)
User avatar
NicTheQuick
Addict
Addict
Posts: 1527
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: Copy Structure

Post 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.
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
User avatar
Piero
Addict
Addict
Posts: 1040
Joined: Sat Apr 29, 2023 6:04 pm
Location: Italy

Re: Copy Structure

Post 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…
Post Reply