1 registered members (AndrewAMD),
677
guests, and 2
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
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
OP
Senior Member
|
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:
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:
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@69Why?I only know this way round:
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
Member
|
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
Expert
|
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:
#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: Clemens]
#383005
09/14/11 22:35
09/14/11 22:35
|
Joined: Sep 2003
Posts: 303 Germany
Clemens
OP
Senior Member
|
OP
Senior Member
Joined: Sep 2003
Posts: 303
Germany
|
Just to go sure, your source implemented in mine above... should look like this:
#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: TechMuc]
#383010
09/14/11 23:43
09/14/11 23:43
|
Joined: Sep 2003
Posts: 303 Germany
Clemens
OP
Senior Member
|
OP
Senior Member
Joined: Sep 2003
Posts: 303
Germany
|
cool, that works... of course it has to be sys_mallocyeah, I'm agree with the "->"/"." critic! relating to me and to the lite-c compiler (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.
|
|
|
|