Gamestudio Links
Zorro Links
Newest Posts
Data from CSV not parsed correctly
by EternallyCurious. 04/18/24 10:45
StartWeek not working as it should
by Zheka. 04/18/24 10:11
folder management functions
by VoroneTZ. 04/17/24 06:52
lookback setting performance issue
by 7th_zorro. 04/16/24 03:08
zorro 64bit command line support
by 7th_zorro. 04/15/24 09:36
Zorro FIX plugin - Experimental
by flink. 04/14/24 07:48
Zorro FIX plugin - Experimental
by flink. 04/14/24 07:46
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
1 registered members (AndrewAMD), 677 guests, and 2 spiders.
Key: Admin, Global Mod, Mod
Newest Members
EternallyCurious, howardR, 11honza11, ccorrea, sakolin
19047 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Parsing a struct to a function, not a pointer to it #382997
09/14/11 21:46
09/14/11 21:46
Joined: Sep 2003
Posts: 303
Germany
Clemens Offline OP
Senior Member
Clemens  Offline OP
Senior Member

Joined: Sep 2003
Posts: 303
Germany
Sry, I know I've still to learn a lot about structs/pointer/etc. Somewhen I will go through a good book... till then there are questions like the following, for some of you sounding stupid or at least obviously/simple ... anyway:

--------------
this works, native:
Code:
function demonstration(var my_own_var) {
	...
	my_own_var = 37;	// do whatever I want with it, nobody cares
	...
}
...
	var number_for_everyone = 1;
	demonstration(number_for_everyone);
	...
	number_for_everyone = 7;
	demonstration(number_for_everyone);
	...
	demonstration(number_for_everyone);
...



this not:
Code:
function demonstration(ANY_STRUCT my_own_struct) {
	...
	my_own_struct.x = 37;	// do whatever I want with it, nobody cares?
	...
}
...
	ANY_STRUCT struct_for_everyone;
	struct_for_everyone.x = 1;
	demonstration(struct_for_everyone);
...


Compiler: Syntax error: can not push STRUCT@69

Why?

I only know this way round:
Code:
function demonstration(ANY_STRUCT* temp_my_own_struct) {
	ANY_STRUCT my_own_struct;
	my_own_struct.x=temp_my_own_struct.x;
	...
	my_own_struct.x = 37;	// do whatever I want with it, nobody cares! 
	...
}
...
	ANY_STRUCT struct_for_everyone;
	struct_for_everyone.x = 1;
	demonstration(struct_for_everyone);
...



Any better solutions?

At least a possibility to copy the whole dataset of a struct into a new one. Like vec_set but for every struct? (okay, I could write an own function or define for it...)

Regards,
Clemens

Re: Parsing a struct to a function, not a pointer to it [Re: Clemens] #383002
09/14/11 22:08
09/14/11 22:08
Joined: Jan 2011
Posts: 122
GUILIN , CHINA
tzw Offline
Member
tzw  Offline
Member

Joined: Jan 2011
Posts: 122
GUILIN , CHINA
passing pointers maybe for performance reasons.
using memcpy for coping a memory field to another byte by byte.
you can look into "c standard lib" for details



Full of my eyes are class struggles.....
Re: Parsing a struct to a function, not a pointer to it [Re: Clemens] #383003
09/14/11 22:13
09/14/11 22:13
Joined: Oct 2004
Posts: 4,134
Netherlands
Joozey Offline
Expert
Joozey  Offline
Expert

Joined: Oct 2004
Posts: 4,134
Netherlands
You can give basic variables like int, float, var, char, ... as function arguments, but not whole structures as they are too large to pass. I'm not sure about the specifics, but I know that in real C, execution will have a considerable impact on speed when passing large amounts of bytes as argument. You are supposed to pass pointers only. And if you need to copy a structure, either create a copy structure function, or use this generic definition:

Code:
#define clone_struct( source, type ) memcpy( sys_alloc( sizeof( type ) ), source, sizeof( type ) )
...
Type* source_struct = sys_alloc( sizeof( Type ) );
Type* cloned_struct = clone_struct( source_struct, Type );


Not extensively tested but it should work...

Pay attention when you use this though. You might actually need a more specific copy_object function. If a struct contains pointers to other structures that you also need to have copied, its best to create your own copy_object function that does this.

Last edited by Joozey; 09/14/11 22:17.

Click and join the 3dgs irc community!
Room: #3dgs
Re: Parsing a struct to a function, not a pointer to it [Re: Joozey] #383004
09/14/11 22:27
09/14/11 22:27
Joined: Sep 2003
Posts: 303
Germany
Clemens Offline OP
Senior Member
Clemens  Offline OP
Senior Member

Joined: Sep 2003
Posts: 303
Germany
I see, it's just the size... okay, doesn't make that much sense for me, because it has to be created anyway, furthermore I also can parse ~unlimited (?) var parameters - but okay!
Thanks for these quick answers!! Going to try that generic definition. thx!

Re: Parsing a struct to a function, not a pointer to it [Re: Clemens] #383005
09/14/11 22:35
09/14/11 22:35
Joined: Sep 2003
Posts: 303
Germany
Clemens Offline OP
Senior Member
Clemens  Offline OP
Senior Member

Joined: Sep 2003
Posts: 303
Germany
Just to go sure, your source implemented in mine above...
should look like this:

Code:
#define clone_struct( source, type ) memcpy( sys_alloc( sizeof( type ) ), source, sizeof( type ) )

function demonstration(ANY_STRUCT* temp_my_own_struct) {
	ANY_STRUCT* temp_my_own_struct = sys_alloc( sizeof( ANY_STRUCT ) );
	ANY_STRUCT* my_own_struct = clone_struct( temp_my_own_struct, ANY_STRUCT );
	...
	my_own_struct.x = 37;	// do whatever I want with it, nobody cares! 
	...
}
...
	ANY_STRUCT struct_for_everyone;
	struct_for_everyone.x = 1;
	demonstration(struct_for_everyone);
...



Okay, I'm going to test it... even I'm still not happy about that quite long solution... maybe I'll write a specific function/define for the struct.

Or does anybody know a more professional way?

Re: Parsing a struct to a function, not a pointer to it [Re: Clemens] #383007
09/14/11 22:49
09/14/11 22:49
Joined: Jul 2008
Posts: 894
T
TechMuc Offline
User
TechMuc  Offline
User
T

Joined: Jul 2008
Posts: 894
eine einfache allokation reicht laugh dafür steht das sys_alloc ja im präprozessor

Code:
function demonstration(ANY_STRUCT* temp_my_own_struct) {
	ANY_STRUCT* copy = clone_struct( temp_my_own_struct, ANY_STRUCT );
	......
        copy->x = 12;
        sys_free(copy);
}



Re: Parsing a struct to a function, not a pointer to it [Re: TechMuc] #383008
09/14/11 22:52
09/14/11 22:52
Joined: Jul 2008
Posts: 894
T
TechMuc Offline
User
TechMuc  Offline
User
T

Joined: Jul 2008
Posts: 894
oh english.. hm..

please get used to use "->" instead of "." (or better said, use them both, in the right context.. -> for pointer, "." for values.. your code is really hard to read this way, and it will for sure not help you to learn those things faster if you rely on the shitty lite-c compiler to resolve such things )

Re: Parsing a struct to a function, not a pointer to it [Re: TechMuc] #383010
09/14/11 23:43
09/14/11 23:43
Joined: Sep 2003
Posts: 303
Germany
Clemens Offline OP
Senior Member
Clemens  Offline OP
Senior Member

Joined: Sep 2003
Posts: 303
Germany
cool, that works... of course it has to be sys_malloc

yeah, I'm agree with the "->"/"." critic! relating to me and to the lite-c compiler wink
(would be easier to learn if the compiler would't do it on its own... btw, can it always know the right way? so does se of ./-> defacto make no difference, and liteC just uses this fact to make it easier for the programer?)

Last edited by Clemens; 09/14/11 23:44.
Re: Parsing a struct to a function, not a pointer to it [Re: Clemens] #383019
09/15/11 07:57
09/15/11 07:57
Joined: Feb 2011
Posts: 135
Myrkling Offline
Member
Myrkling  Offline
Member

Joined: Feb 2011
Posts: 135
You can use #define PRAGMA_POINTER to inhibit lite-C from detecting pointers automatically.

Just insert this line in your main script, right after the default includes.

Re: Parsing a struct to a function, not a pointer to it [Re: Myrkling] #383022
09/15/11 08:27
09/15/11 08:27
Joined: Sep 2003
Posts: 303
Germany
Clemens Offline OP
Senior Member
Clemens  Offline OP
Senior Member

Joined: Sep 2003
Posts: 303
Germany
very good hint! thank you


Moderated by  HeelX, Lukas, rayp, Rei_Ayanami, Superku, Tobias, TWO, VeT 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1