Copying a struct

Posted By: FoxHound

Copying a struct - 06/11/12 00:33

I think I may not understand fully how memcpy works.

So I have a struct which has several panels in it which You can see on screen. I loaded one up and wanted to simply copy that struct to a new one. I used memcpy the way it shows in the manual but I still only have one visible panel on screen. Which now behaves the way I set the second one two. I made several of these, 70, and still one panel and all behave the way one of them should. i can tell all of the functions are working but they are not moving the panel at 70 times the rate of speed as I would expect.

Any helpful advice that my point me in the right direction?
Posted By: EvilSOB

Re: Copying a struct - 06/11/12 02:52

We need some code dude... Or at least post the struct you are trying to copy.

There are several reasons it could be failing...
Posted By: FoxHound

Re: Copying a struct - 06/11/12 03:09

Code:
//declared outside of function
FSA_panel* girlscout_fsa;	
FSA_panel* fsa_yo[150];



function setup_fsa_001()
{
	girlscout_fsa = malloc(sizeof(FSA_panel));
	
	if(!FSA_create("girlscout001.fsa",vector(700,random(500),0),girlscout_fsa))
	{
		printf("file not found");
		sys_exit("");
		return;
	}

}


function copy_fsa()
{
		fsa_yo[0] = malloc(sizeof(FSA_panel));
               memcpy(fsa_yo[0],girlscout_fsa,sizeof(FSA_panel));  

	
}




FSA_create works fine I can add as many of theses as I want with it, all the other FSA_ functions work fine. I want something a bit faster than the FSA_create function. I was hoping for a better understanding of making a copy of one struct to another and working with them independently of each other.
Posted By: EvilSOB

Re: Copying a struct - 06/11/12 03:24

I NEED to see the FSA_panel struct itself, and the 'FSA_create' would be helpful.


BTW, in 'copy_fsa', the memset is just plain wrong, it will just fill the fsa_yo
with junk data.


And the memcpy MIGHT need to be
"memcpy([b]&[/bfsa_yo[0], [b]&[/bgirlscout_fsa, sizeof(FSA_panel));"
or
"memcpy([b]&[/bfsa_yo[0], girlscout_fsa, sizeof(FSA_panel));"

I always have trouble myself determining if the '&' is necessary and need
to "trial and error" test it every time I use it...
Because sometimes it is, and sometimes not, depending on the pointers declaration.
Posted By: FoxHound

Re: Copying a struct - 06/11/12 03:30

Some of the code I put up at first was a bit off as I do a lot of trail and error, basically I try break it when it doesn't do what I want.

Neither way I use the "&" symbol works, as in the game crashes bad both ways.

The struct and FSA_create works just fine. I want a quick and easy way to copy the struct.

I'm making this.
http://www.opserver.de/ubb7/ubbthreads.php?ubb=showflat&Number=402345#Post402345
The struct itself is huge.
Posted By: EvilSOB

Re: Copying a struct - 06/11/12 04:07

It doesnt matter that the struct is huge, I will only be skimming through it.
AND it doesnt matter (to me) that fsa_create works fine as is either.

I need to see them because SOME types of data will memcpy fine, and others dont.
Im looking for types that dont copy, and need re-creating...


You can PM the info to me if you want to keep it confidential, I dont mind.
Posted By: MasterQ32

Re: Copying a struct - 06/11/12 05:16

i assume you use engine panels
so copying one of your structs with the PANEL* pointer does NOT copy the panel but the reference to it. so you are currently really using only one panel. to copy a PANEL* you need to create a new panel and set all parameters in your code, memcpy would fail here because of the internal engine layout
Posted By: EvilSOB

Re: Copying a struct - 06/11/12 07:39

Master32 has hit ONE of the types of data Im looking for, but there are others.

But even then, its unlikelt to cause problems, you just need to handle
those 'special' elements properly.

Like the PANEL that Master has brought up.
It CAN be 'memcpy'ed, BUT you need to do a pan_create first, and then
memcpy from the old panel ... always SKIPPING the first FOUR bytes, and maybe the last 24...
This way youve done a 'quick' copy of its contents, without disturbing the
internal engine values.
Posted By: FoxHound

Re: Copying a struct - 06/11/12 15:41

That answers my question then. The reason fsa_create takes so long is that it has to create up to 32 panels. In the example I'm doing there are only 17 panels. It either freezes the game for a second or with several waits put into key places to prevent a freeze it takes 3 seconds to create a new FSA. I'll play around with some new ideas today and see if i can fix it.
Posted By: FoxHound

Re: Copying a struct - 06/11/12 19:25

Still never found an answer to my problem but I solved my problem another way. Apparently I decided to create a panel 256 times instead of just the one I needed. Multiply that across 17 panels and it was quite a mess. So all the waits remove and this thing loads fast.
© 2024 lite-C Forums