I have a data struct, that will be read in a function.
I want the smallest memory, code size, and speed footprint possible. I'm working on AVR.
typedef struct {
uint16_t clu;
uint16_t num;
uint32_t cur_rel;
} FSAVEPOS;
Now, a function that stores file position into the struct:
// Approach 1
FSAVEPOS save_pos(const FFILE* file); // return value
// Approach 2
void save_pos(const FFILE* file, FSAVEPOS* pos); // modify reference
And a function that reverses this (FFILE object is modified):
// Approach 1
void restore_pos(FFILE* file, const FSAVEPOS pos); // pass by value
// Approach 2
void restore_pos(FFILE* file, const FSAVEPOS* pos); // pass by reference
What would you advise as the best idea?
In the first case, when you return a struct in C, it's usually returned in a register if it fits or is implicitly passed by reference and modified AFAIK. So, in the best case it's better and in the worst case is equal than the second approach.
In the second case it depends on the size of a pointer and the size of the struct among other things. In this case is very difficult to say anything without measurement. It probably won't be much different with a struct of that size, though.
However you should consider also having consistency with the rest of your API, and having a system to notify errors if you can't do the operation (if the operation can fail).