how to copy a whole structure content to another

Just starting out? Need help? Post your questions and find answers here.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by enisel.

Hello,

This code doesn't generate error, but the line

strb = stra

doesn't copy the whole structure content stra to strb.
Is there a way to perform this easily?

Thanks for your help.
Enisel



openconsole()
structure test
sa.l
sb.l
endstructure

stra.test
strb.test

stra\sa = 10
stra\sb = 20

strb = stra

printn(str(stra\sa))
printn(str(stra\sb))
printn(str(strb\sa))
printn(str(strb\sb))

input()
CloseConsole()
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Berikco.

You will have to copy each element in a loop.

strb\sa = stra\sa
strb\sb = stra\sb

A CopyListItem() function is on Fred's todo list (i hope) :)

Regards,

Berikco

http://www.benny.zeb.be/purebasic.htm
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by freak.

I always do it with CopyMemory. A Structure is nothing else than a Block of
allocated Memory, and can be used with all the Memory Commands. The addresses of the
Structured Variables can be found with the '@' in front of them.

Code: Select all

openconsole()
structure test
sa.l
sb.l
endstructure

stra.test
strb.test

stra\sa = 10
stra\sb = 20

; here we copy the contents:

CopyMemory( @stra, @strb, SizeOf( test ) )

; Some Notes:
; The first is the source, the second one the destination Address.
; the 'SizeOf( test )' command returns the Size of the Structure.
; Note that not like some other Languages, you have to use the Name of the
; Structure with SizeOf(), not the name of a Variable.
; (This is also on Fred's list, i think)
;
; The good thing about this way of copying is, if you now change the 'test' Structure, for
; example by adding a new Long Value to it, the CopyMemory() wil automatically copy this one too,
; without any further changes to be made.

printn(str(stra\sa))
printn(str(stra\sb)) 
printn(str(strb\sa))
printn(str(strb\sb)) 

Input() ; wait for  Enter Key...

That's it...

Timo
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by horst.
Originally posted by freak

I always do it with CopyMemory.
It should be pointed out, however, that this does not copy
strings in a structure. It copies only the pointers,
so you will have one common string: if you change it in one
of the structures it will also be changed in the other one.

And what happens, if the original structure was in a linked
list, and you delete the element?


Horst
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Pupil.

One small note about the copy procedure...

Just look out if there's a string in the structure, because then you just copy the pointer to the string, which means that you actually doesn't have a copy of the string content. This result, with two pointers in the structures pointing to the same memory address, could very well lead to disaster(or unwanted, unpredictible results) if one or both of these strings in the structures were to be manipulated in any way.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Berikco.

Nothing to add Horst and Pupil :)

I use linked list for about everything now, my current project is full of linked list

I hope Fred will add commands to copy ListElements once :) (lets hope not in five years)

Regards,

Berikco

http://www.benny.zeb.be/purebasic.htm
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by fred.

Damn, Bericko is pressuring me :).

Fred - AlphaSND
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by enisel.

Thanks for your help.
CopyMemory works fine! (I have no strings in my structures)
Enisel
Post Reply