Storage classes

What is the difference between a variable declared as auto and static ? What is the difference in memory allocation in auto and static ? Why are we using static with an array of pointers and what is its meaning?

+4
source share
3 answers

AUTO (default) , Static , Extern, and Register are 4 modifiers for a variable in C.


  • AUTO: By default. Normal variable.

  • STATIC : Changes the lifetime of a variable. (retains volume, no change).

    This means that at run time, the OS does NOT delete the variable from memory after the function (containing the variable exits) and initializes the variable each time the function is called.

    Rather, a static variable is initialized ONLY the first time a function (containing it is called). Then it continues to be in memory until the program terminates. in other words, STATIC effectively makes the GLOBAL variable in memory, but has only LOCAL access.

    If your statics are stored depends on whether they were initialized or not.

    • 0 initialized static data is sent to .BSS (Block Started by Symbol),

    • no initialized data is available. DATA

    It should be noted that although static variables are always in memory, they can only be obtained from the local area (the function in which they are defined).

  • EXTERN: Used to tell the compiler that the extern definition is just a placeholder, and the actual definition is in a different place. Declaring a variable as extern will cause your program to not reserve memory for the variable in the area that it declared. It is also common to search for prototypes of functions declared as extern.

  • REGISTRATION: Compiler Signals: It is preferable to use the CPU register (rather than RAM) to store this variable. Used to improve performance when accessing a variable again (for ex: loop counter variables).

+5
source

I assume that if you are talking about auto variables, you probably mean local variables in a function. auto by default, this means that the variable is allocated on the stack when the function is called and freed when the function returns. static means that the variable is allocated the first time the function is called and remains allocated to the rest of the program. It means:

 int foo() { static int x = 0; return x++; } printf("%d\n", foo()); // Outputs 0 printf("%d\n", foo()); // Outputs 1 printf("%d\n", foo()); // Outputs 2 
+2
source

auto: - auto storage class - this is the default class. If we declare a variable inside a function without defining any memory, it automatically rises to an automatic variable. Simply put, we can say that a local variable (not static) is an automatic variable. the scope of an automatic variable lies within the function in which it is declared and acts before control within the function.

eg

  int main() { int a =10; { int b=10; } printf("%d",b); } 

In the above example, a and b are both local variables and live up to control within the main function, but the visibility of a and b in braces where they are defined. When we try to compile the code above, I got an error: - undeclared variable b.

  int main() { int a =10; int *d; { int b=20; d=&b; } printf("%d",*d); } 

Exit 20. because b until the control is in the main function.

static: -
When we use static (prefix) with a variable, it is alive throughout the entire program launch. means the degree of the static variable throughout the program run. the volume of the static variable in the module in which it is declared. If we used static with a global variable, then the global variable was limited to the file in which it was declared. in other words, we can say that static makes a variable or function private to the file in which it is declared.
when we initialized a static variable, then create in the data segment (.ds) or create in .bss (the initial character) of the process.

Note: - a static variable is initialized at compile time, when memory allocates it. If we do not initialize, then it is the compiler's responsibility to initialize it with Zero.

In my experience, when you create a lookup table or any table that requires a full program run and you want to make it a specific file, you declare static.

for more information see this link: http://aticleworld.com/storage-class/

+1
source

Source: https://habr.com/ru/post/1307254/


All Articles