It is currently Tue Apr 13, 2021 11:52 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject:
PostPosted: Sun Jan 19, 2003 3:55 pm 
Offline
PureBasic Guru
PureBasic Guru

Joined: Tue Apr 22, 2003 7:42 pm
Posts: 16777133
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()


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Sun Jan 19, 2003 4:11 pm 
Offline
PureBasic Guru
PureBasic Guru

Joined: Tue Apr 22, 2003 7:42 pm
Posts: 16777133
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


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Sun Jan 19, 2003 5:29 pm 
Offline
PureBasic Guru
PureBasic Guru

Joined: Tue Apr 22, 2003 7:42 pm
Posts: 16777133
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


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Sun Jan 19, 2003 7:42 pm 
Offline
PureBasic Guru
PureBasic Guru

Joined: Tue Apr 22, 2003 7:42 pm
Posts: 16777133
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


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Sun Jan 19, 2003 7:55 pm 
Offline
PureBasic Guru
PureBasic Guru

Joined: Tue Apr 22, 2003 7:42 pm
Posts: 16777133
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.


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Sun Jan 19, 2003 8:35 pm 
Offline
PureBasic Guru
PureBasic Guru

Joined: Tue Apr 22, 2003 7:42 pm
Posts: 16777133
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


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Sun Jan 19, 2003 9:52 pm 
Offline
PureBasic Guru
PureBasic Guru

Joined: Tue Apr 22, 2003 7:42 pm
Posts: 16777133
Restored from previous forum. Originally posted by fred.

Damn, Bericko is pressuring me :).

Fred - AlphaSND


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Mon Jan 20, 2003 12:59 am 
Offline
PureBasic Guru
PureBasic Guru

Joined: Tue Apr 22, 2003 7:42 pm
Posts: 16777133
Restored from previous forum. Originally posted by enisel.

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


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 43 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye