| SuperStinger (34) | |||
|
Hi all! I have a struct that i want to return from a function, but i think it is a bit inefficient to return a really big struct, so i want to return its pointer. Can i create the struct variable inside the function and then return its pointer, or would the pointer be dangling and left leading to nothing.
How would i approach this? Also, would i be able to modify a function argument, is there anything wrong with that? So if created the struct variable outside of the function ( in main ) and then passed it as an argument, which was a pointer, then i modify that? Many thanks, SuperStinger | |||
|
|
|||
| Framework (3237) | |||
Never return the address of automatic storage. The safer option is to make a static pointer within the function and return that instead.
This has it's drawbacks, but does ensure that "Font_" remains valid until the program ends. With each call to the function, the address pointed-to by "Font_" can change. This is the safest way of returning the address of storage that's declared within a function's scope. Note that static storage declared within a function scope remains even if the function is not currently being executed. Wazzak | |||
|
Last edited on
|
|||
| darkestfright (1128) | |||
Your loadFont method should probably be a member function of the font struct.
| |||
|
|
|||
| SuperStinger (34) | |
|
thanks for your responses @darkestfright: from the loadFont function that i would put in the typedef struct, how would i access the variables inside the struct ( i.e bmp and symbol ) EDIT: ive tried various things by putting the function inside the typedef, but it gives me errors, i dont know how to do this, can someone show me please? many thanks EDIT2: dont worry, im writing in pure C, not C++, so it doesnt like that, ill just make functions outside of the typedef, thanks for your help | |
|
Last edited on
|
|
| JLBorges (1756) | |
|
> but i think it is a bit inefficient to return a really big struct, > so i want to return its pointer. No, it is not. Every current mainstream compiler implemts NRVO. http://en.wikipedia.org/wiki/Return_value_optimization http://cpp-next.com/archive/2009/08/want-speed-pass-by-value/ | |
|
|
|
| darkestfright (1128) | ||
You should be more clear about that in the future then. | ||
|
|
||
| kev82 (323) | |||
| |||
|
|
|||