Copy structure with pointer (v4.40 work around)

Share your advanced PureBasic knowledge/code with the community.
User avatar
Demivec
Addict
Addict
Posts: 4270
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Copy structure with pointer (v4.40 work around)

Post by Demivec »

Code updated for 5.20+ (same as CopyStructure())

PureBasic v4.40b7 now supports a simple way to copy structures with

Code: Select all

this.test = that.test  ;works great
but this does not work when using pointers.

Code: Select all

*this.test = *that.test  ;doesn't work for copying structure's contents
Here's a simple idea to get around this and it only costs an additional structure definition.

The simple code below copies the 'test' structure via pointers by using the 'work' structure so that the structure will be dereferenced for copying:

Code: Select all

;original structure
Structure test
  index.i
  phrase.s
EndStructure

;work structure for copying
Structure work
  test.test[0]
EndStructure

Procedure copyByPtr(*a.work,*b.work)
  *b\test = *a\test
EndProcedure

Define V1.test, V2.test

V1\index = 5
V1\phrase = "hello"

Debug "----source----"
Debug V1\index
Debug V1\phrase

Debug "----destination contents before----"
Debug V2\index
Debug V2\phrase

copyByPtr(@V1,@V2)
Debug "----destination contents after----"
Debug V2\index
Debug V2\phrase
Hopefully this work around won't be necessary in v4.50. :wink:
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: Copy structure with pointer (v4.40 work around)

Post by srod »

Pretty neat. 8)
I may look like a mule, but I'm not a complete ass.
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Re: Copy structure with pointer (v4.40 work around)

Post by Mistrel »

Why would you expect *this.test = *that.test to work? :?
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: Copy structure with pointer (v4.40 work around)

Post by srod »

He is talking about copying the structure pointed to by one pointer to the structure pointed to by another pointer.

Although you have a good point there in that *ptr1 = *ptr2 is always going to be a shallow copy of pointers (as opposed to a 'deep copy' if the underlying structure memory).

@Demivec, you will always need a workaround of sorts because *this.test = *that.test MUST absolutely remain a shallow copy of pointers.
I may look like a mule, but I'm not a complete ass.
User avatar
Demivec
Addict
Addict
Posts: 4270
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Copy structure with pointer (v4.40 work around)

Post by Demivec »

[quote = "Mistre"]Why would you expect *this.test = *that.test to work?[/quote]
@Mistrel: I wouldn't. I am familiar with how an assignment works between pointer variables. I don't want that to change. What I do want is that there be a way to copy the contents of a structure, whose address I have in a structured pointer, to another another address that is pointed to by another structured pointer.
srod wrote:He is talking about copying the structure pointed to by one pointer to the structure pointed to by another pointer.

Although you have a good point there in that *ptr1 = *ptr2 is always going to be a shallow copy of pointers (as opposed to a 'deep copy' if the underlying structure memory).

@Demivec, you will always need a workaround of sorts because *this.test = *that.test MUST absolutely remain a shallow copy of pointers.
@srod: regarding always vs. temporary workarounds, I am hoping that some of the suggestion that have been made regarding this are made effective. According to my notes there are basically two suggestions, one is to implement a compile-time function that looks like this:

Code: Select all

CopyStructure(*source,*destination,structure)
the other suggestion is to use the structure punction like this:

Code: Select all

*source\ = *destination\ ;
I favor the first one, though technically, either of these would be fine with me. :D
User avatar
Rescator
Addict
Addict
Posts: 1769
Joined: Sat Feb 19, 2005 5:05 pm
Location: Norway

Re: Copy structure with pointer (v4.40 work around)

Post by Rescator »

Copying the structure from one pointer to the structure of another pointer makes no sense and sounds dangerous even.

Wouldn't "this.test = *that.test" be much more logical? (does that work?, haven't tested yet!)

PS! I kinda like the CopyStructure() as well, and it's much more explicit. (the other way is too accident prone, a simple typo and you'd be copying strucures who knows where).
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: Copy structure with pointer (v4.40 work around)

Post by srod »

"this.test = *that.test"
Presently that simply copies the pointers and really should not be changed else we'd be screwing up pointer arithmetic and the like. A deep structure copy performed through pointers should be done, inmo, through some other means, through one of the methods outlined by Demivec perhaps.
I may look like a mule, but I'm not a complete ass.
User avatar
einander
Enthusiast
Enthusiast
Posts: 744
Joined: Thu Jun 26, 2003 2:09 am
Location: Spain (Galicia)

Re: Copy structure with pointer (v4.40 work around)

Post by einander »

Is'nt simpler this :?:

Code: Select all

 Structure test
   Index.i
   phrase.s
EndStructure

Define V1.test, V2.test
   
V1\Index = 5
V1\phrase = "hello"
   
Debug "----source----"
Debug V1\Index
Debug V1\phrase
   
Debug "----destination contents before----"
Debug V2\Index
Debug V2\phrase
  
CopyMemory(@V1,@V2,SizeOf(test))
   
Debug "----destination contents after----"
Debug V2\Index
Debug V2\phrase
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: Copy structure with pointer (v4.40 work around)

Post by srod »

Doesn't copy strings; only their pointers. Free one structure and you will invalidate the string pointer in the other.
I may look like a mule, but I'm not a complete ass.
Seymour Clufley
Addict
Addict
Posts: 1265
Joined: Wed Feb 28, 2007 9:13 am
Location: London

Re: Copy structure with pointer (v4.40 work around)

Post by Seymour Clufley »

srod wrote:Presently that simply copies the pointers and really should not be changed else we'd be screwing up pointer arithmetic and the like. A deep structure copy performed through pointers should be done, inmo, through some other means...
Surely the best, simplest, most obvious way to do a deep structure copy would be with a command:

Code: Select all

CopyStructure(@a,@b)
Doing it with a=b is just confusing, IMHO.
JACK WEBB: "Coding in C is like sculpting a statue using only sandpaper. You can do it, but the result wouldn't be any better. So why bother? Just use the right tools and get the job done."
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: Copy structure with pointer (v4.40 work around)

Post by srod »

I would say that that could be implemented quite easily since we now have structure copying added to PB through a = b etc.
I may look like a mule, but I'm not a complete ass.
User avatar
Demivec
Addict
Addict
Posts: 4270
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Copy structure with pointer (v4.40 work around)

Post by Demivec »

einander wrote:Is'nt simpler this :?:
@einander: It is simpler but as srod pointed out, it doesn't accomplish a deep copy. Secondly, the original code example was rigged so that it would demonstrate doing a copy with pointers. Otherwise it could have been done even simpler with V2.test = V1.test. :wink:

I realize people may not even realize that structure copying is now native as of the v4.40 betas. That's one reason I posted this thread. It shows one way to utilize this feature with structure pointers and the structure data they point to.
Polly
User
User
Posts: 29
Joined: Sat Jan 19, 2008 10:31 pm

Re: Copy structure with pointer (v4.40 work around)

Post by Polly »

Maybe this is the best workaround for the time being? Only only one init command per structure (+1 generic macro):

Code: Select all

Macro InitCopyStruct(struc)

   Structure work#struc
     struc.struc[0]
   EndStructure
   
   Procedure copyStruct#struc(*a.work#struc,*b.work#struc)
     *b\struc = *a\struc
   EndProcedure

EndMacro

;original structure
Structure test
  index.i
  phrase.s
EndStructure

Structure test2
  index.i
  phrase.s
EndStructure

InitCopyStruct(test)
InitCopyStruct(test2)


Define V1.test, V2.test, V3.test2

V1\index = 5
V1\phrase = "hello"

Debug "----source----"
Debug V1\index
Debug V1\phrase

Debug "----destination contents before----"
Debug V2\index
Debug V2\phrase

copyStructTest(@V1,@V2)
Debug "----destination contents after----"
Debug V2\index
Debug V2\phrase

*pV2.test=@V2
*pV3.test2=@V3
copyStructTest2(*pV2,*pV3)
Debug "----destination contents after----"
Debug V3\index
Debug V3\phrase
User avatar
einander
Enthusiast
Enthusiast
Posts: 744
Joined: Thu Jun 26, 2003 2:09 am
Location: Spain (Galicia)

Re: Copy structure with pointer (v4.40 work around)

Post by einander »

@Demivec:
I realize people may not even realize that structure copying is now native as of the v4.40 betas.
I`ve missed this.
Thanks for the tip!
@Srod:
:oops:
Post Reply