Thread: global struct variable best use

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    147

    global struct variable best use

    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

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Use the global variable, native - it's not the best for safe software development, but it is the fastest.

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    All three versions are still using a global. The fact that in two cases you also access it through something that is local doesn't change the fact that these functions rely on a global.

    When you read local vars are preferred that would mean that you have an instance of this struct in main (or any other top-level function) and pass (a pointer to) it around through function arguments. (But this is a code design issue and not a performance issue.)

    Regarding performance, the third obviously is slower, and the second is rather pointless (a global is a global, no matter how you refer to it).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 12:37 AM
  2. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  3. Replies: 1
    Last Post: 12-03-2008, 03:10 AM
  4. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

Tags for this Thread