PureBasic Forum
https://www.purebasic.fr/english/

how to copy a whole structure content to another
https://www.purebasic.fr/english/viewtopic.php?f=13&t=4832
Page 1 of 1

Author:  BackupUser [ Sun Jan 19, 2003 3:55 pm ]
Post subject: 

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()

Author:  BackupUser [ Sun Jan 19, 2003 4:11 pm ]
Post subject: 

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

Author:  BackupUser [ Sun Jan 19, 2003 5:29 pm ]
Post subject: 

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:
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

Author:  BackupUser [ Sun Jan 19, 2003 7:42 pm ]
Post subject: 

Restored from previous forum. Originally posted by horst.

Quote:
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

Author:  BackupUser [ Sun Jan 19, 2003 7:55 pm ]
Post subject: 

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.

Author:  BackupUser [ Sun Jan 19, 2003 8:35 pm ]
Post subject: 

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

Author:  BackupUser [ Sun Jan 19, 2003 9:52 pm ]
Post subject: 

Restored from previous forum. Originally posted by fred.

Damn, Bericko is pressuring me :).

Fred - AlphaSND

Author:  BackupUser [ Mon Jan 20, 2003 12:59 am ]
Post subject: 

Restored from previous forum. Originally posted by enisel.

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

Page 1 of 1 All times are UTC + 1 hour
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/