Design question about global variable

Hello

Normall in an application we define global variable as:
int a;
int b;
...

How about use one structure to include all variables, and just define one structure instance? as below:
struct{ int a; int b; ...
}global;

Is the performance slower? does it take more code space? Is there any other drawback?

Thanks.

/Wang

Parents
  • Is this a trick to get fewer "global variables"?

    The access to that big struct is quite efficient as long as a register is pointing at the struct and the code then specifies the offset of each field.

    So specially for Thumb-mode code, this can work very well. But note that Thumb and Thumb2 have different limits for how large offsets they can span.

    Write the code so _you_ feel it is easy to maintain. Group globals in structs or close together if they relate to each other. Preferably see if they can get names that help to tell they inter-relate.

    But don't use "number of globals" as your main design criteria.

Reply
  • Is this a trick to get fewer "global variables"?

    The access to that big struct is quite efficient as long as a register is pointing at the struct and the code then specifies the offset of each field.

    So specially for Thumb-mode code, this can work very well. But note that Thumb and Thumb2 have different limits for how large offsets they can span.

    Write the code so _you_ feel it is easy to maintain. Group globals in structs or close together if they relate to each other. Preferably see if they can get names that help to tell they inter-relate.

    But don't use "number of globals" as your main design criteria.

Children
More questions in this forum