C time and space distribution of variable variables

If I have a test.c file with the following

#include ...
int global = 0;

 int main() {
 int local1 = 0;

  while(1) {
  int local2 = 0;
  // Do some operation with one of them
 }
 return 0;
}

So, if I had to use one of these variables in a while loop, which one is preferable?

I may be a little vague, but I want to know if there really is a difference in the distribution of time / space.

+3
source share
5 answers

, for / , . , malloc ed - . , .

- . , , , .

+5

, : , . , .

+2

, , , . , ( ) .

, - . , .

+2

C , . , "Inter-Processural Flow Flow Analysis", .

, . , , . ( "" ) , ( , , ), " " "", , , .

( ) .

, , -- . , , , , .

, , , . .

+2

: static (), () .

CPU. - , . , ( 8 32 , ).

, . , 1-4 .

, , . , ; , . , , , , . , , , .

- . , , . , , , , , .

, . , , . , , . , .:)


: :

Register variables are allocated by the compiler, so there is no runtime overhead. The code will simply put the value into the register as soon as the value is generated.

Stack variables are allocated by your program at runtime. Typically, when a function is called, the first thing it will do is reserve enough stack space for all of its local variables. Thus, there is no variable cost.

+2
source

All Articles