Storage classes in C

S

The storage class (memory) shows when, how, and where memory is allocated for a variable (vector). Any variable C has a memory class that results either from an explicit statement or implicitly from where the variable is defined.

The memory area used by a C program includes four sub-zones:
1. Text area: where the program code is stored.
2. Data area: where global variables are allocated.
3. Stack area: where temporary data (local variables)
4. Heap zone: where dynamic memory allocations are made

There are two types of memory classes:
1. Static: memory is allocated to compilation in the data segment within the program and can no longer be changed during execution. External variables, defined outside functions, are statically implicit but can be declared static and local variables defined within functions.

2. Auto: The memory is automatically allocated when a function is activated in the stack area assigned to a program and is automatically released when the function is finished. The local variables of a block (a function) and the formal arguments are implicit in the auto class.

Static variables can be initialized only with constant values ​​(because they are compiled), but auto variables can be initialized with the result of expressions (because they are executed). All external (and static) variables are automatically initialized with zero values ​​(including vectors). The amount of memory allocated for variable variables is automatically derived from the type of the variable, and the vector size declared. Dynamically allocated memory is explicitly specified as a parameter of allocation functions.

A variable or a statically declared function has a lifetime equal to that of the program. Consequently, a static variable declared in a function retains its value between successive calls of the function, unlike the auto variables that are reallocated on the stack at each call of function and start each time with the value received at their initialization or with an unpredictable value if they are not initialized. Static local variables are very rarely used in practice when talking about programming . The strtok library function is an example of a function with a static variable.
Static variables can only be initialized with constant values, because initialization occurs at compilation, but auto variables can be initialized with the result of expressions because initialization occurs at execution.

The local variables of a block (a function) and formal parameters are implicit in the auto class. The life of these variables is temporary: memory is allocated automatically when the block/function is activated in the stack area assigned to the program and is automatically released at the block exit/end of the function. Local variables are NOT initialized, and we have to assign them an initial value. The third class of memory is the register class for variables that, theoretically, are assigned CPU registers rather than memory locations for better access time. In practice, no modern compiler disregards this keyword, automatically using registers when the code can be optimized in this way, for example, when it is noticed that the address of the variable is never accessed in the program. Memory unoccupied by static data and program instructions are divided between stack and heap. Memory consumption per stack is higher in programs with recursive functions and a large number of recursive calls, and heap memory consumption is high in dynamically assigned (and reallocated) vector and matrix programs.

Recent Posts

Archives

Categories