views:

1367

answers:

6

I would like to know difference between static variables and global variables in terms of access speed and space consumption. (If you want to know my platform: gcc compiler on Windows. (I am using Cygwin with Triton IDE for ARM7 embedded programming on windows. Triton comes with gcc compiler on Java platform which can be run on Windows.))

(Obviously I know in terms of file and function scope from this question)

Edit: OK give me an answer on any micro controller / processor environment.

+1  A: 

Space consumption: basically no difference. The only time there'd be a space issue is if you manage to get the same chunk of static data hidden in N object files, then you get a multiplication factor of N where you might have just 1 copy if it was a single global piece of data. However, that's a mis-design issue. Information hiding is good - unless the information should not be hidden.

Access speed: no difference.

Jonathan Leffler
+1  A: 

It's hard to guess, or to estimate. It would probably take time, but I would make a sample project and test for speed. Testing both access speed and space with a loop. Test the sample project with an emulator for that architecture.

gonegonegone
Looking at the assembly produced by the compiler would be revealing as well.
Judge Maygarden
+1  A: 

I would expect any difference would come from packing (for space) and caching (for speed) issues. Both those could also arise from just about anything else as well.

BCS
+8  A: 

There is no difference for the space, they take the same amount.

But there is a speed difference: static is faster.

Of course the memory access to the variable is for global and static the same. But the compiler can optimize when you have static. When it compiles a module it knows that no function call to a function outside the module can change a static variable. So it knows exactly what happens and can e.g. keep it in a register over function calls. When it is global and you call a function from a different module, the compiler can't know what it does. Hence he must assume that the function accesses the variable and changes it, resulting in a store and reload.

With gcc you can pass all .c sources at the same time, so it can then also see what happens in function calls to functions from different modules. To make it work you have to pass besides all .c files at once -combine and -fwhole-program. The -fwhole-program makes all globals static (not module static but compilation unit static, i.e. all the given .c files together). The -combine makes the intermodule analysis.

flolo
A: 

There is no difference in the env you describe when it comes to space. The static or global var consume just the same amount of memory.

For speed considerations (but not good practice) you could prefer global vars, if you need access to the var outside the one file. (ref use of external char my_global_char_placed_else_where;)

For better practice you use get/set functions instead but they are slower. So then you could use macros for get/set of a var that is global to hide from the reader of the code that the var is in fact global, but that is kind'a like cheating. But it can make the code more readable.

If you compare hiding a var inside a function, then it has no difference compared with placing it outside the function and more functions could have access to the var.

I myself use MSP430, ARM7(just for tests) and AVR32 micros for development

eaanon01
Thank you Jonathan Leffler
eaanon01
A: 

What Jonathan says is not exactly correct. Both static and global variables will be (has to be) saved in the ZI (or RW data) regions. The compiler cant "keep" it over the register strictly - what it might do is load the value into the register, use that register for all operations and than save that value back - thats a compiler specific optimization. And even then, there is no reason why the compiler wont do that also for global variables : unless of course u make it volatile. But then, technically you can also make a static variable volatile, so again no difference.

Edit : oh yeah - space : no difference.