copy method / memcopy problem

BlitzMax Forums/BlitzMax Programming/copy method / memcopy problem

Ghost Dancer(Posted 2010) [#1]
I've been trying to get a method working to copy an instance of a type. This is based on some code I found in an old post.

Method copy:TTemplate()
	Local newTemplate:TTemplate = New TTemplate
	
	MemCopy (Byte Ptr(newTemplate), Self, SizeOf(Self))
	
	Return newTemplate
End Method


usage:

myTemplate = template.copy()


Howver, after copying I get array out of bounds errors (for the array within the type) and also bad reference errors. I'm guessing there is something wrong in the way it is allcoating the memory but not sure how to fix it.

I could just copy each individual field & array element but it would be more elegant if I can get it working with memcopy.


beanage(Posted 2010) [#2]
Make sure you are only having BYTE, SHORT, INT, LONG, FLOAT or DOUBLE and NO ARRAY or OBJECT fields in <Self> and <newTemplate>. Not sure about Strings, but propably not safe either.


Ghost Dancer(Posted 2010) [#3]
Ah, so will this only work if the type has fixed length fields then? Makes sense I suppose, thanks.