Hi,
I haven't developed yet a special routines I need. Before that I would like to know what is the prefered method to do it.
In theses cases, I have a global var struct:
Code:
typedef struct {
int myvar1;
char * myvar2;
int myvar3;
....
....
int myvar50;
} t_myStruct
and defined into a data.c file I have:
Code:
t_myStruct myGlobalData;
This struct is going to widely used into my program in different functions. Of course, I tried to avoid global vars, but this one I need it. It is the excepcion. All others are local var or function argument variables.
Thinking in how to use it to get maximum performance, I have three options:
1) Use the global var "as is" into any function. I.e.:
Code:
int myFunc(void) {
myGlobalData.myvar1 = .....
....
myGlobalData.myvar10 = myGlobalData.myvar5 + .....
....
etc.
}
2) Declare a local pointer and use it:
Code:
int myFunc(void) {
t_myStruct * p;
p = &myGlobalData;
p->myvar1 = .....
....
p->myvar10 = p->myvar5 + .....
....
etc.
}
3) Use local var and then copy to global struct.
Code:
int myFunc(void) {
t_myStruct localStruct;
localStruct.myvar1 = .....
....
localStruct.myvar10 = localStruct.myvar5 + .....
....
etc.
memcpy(localStruct,myGlobalData,sizeof(t_myStruct)
}
I have read using local vars is preferred, but my intuition says me first one is the best, as the compiler knows the exact memory address to save information in each line of code. Anyway I would like to understand what is the preferred method to get maximum performance. Could explain me what is best and why?
Thanks in advance.
Fermin