When writing computer code, there are a lot of things to understand. Each programming language is different, with its own set of syntax and structures. One of the things that are common across many programming languages is the need to set variables. Variables store data in a way that can be referenced in the future.
You can assign different values to a variable, such as strings of text, numbers, or an array of data, amongst many other things. Many languages require you to declare what type of data will be stored in a variable limiting you to only numbers, for example.
Another thing that differs between programming languages is the need to declare the allocation type of a variable. Some languages don’t require this, while others are stringent. There are two main allocation types: Static and Dynamic.
Static vs. Dynamic
A static variable is a variable that has its size determined before the application is ever run. It’s set at compile time by the compiler. For example, a string can be defined that has a definitive length. The memory space needed to store a long string is then statically allocated by the compiler.
The alternative is dynamic allocation. Here the size of the variable is set by the program as it runs. The program contains logic to adjust the amount of memory allocated to the variable, allowing it to change over time if more or less space is needed. This typically offers more flexibility in memory usage than static allocation but does come at a bit of a performance cost. One of the main advantages of dynamic allocation is that it doesn’t need to be allocated until it’s needed. The memory can be freed once it’s no longer needed, even if the program is still running.
Variables that are statically allocated are permanently stored in memory. That’s not to say that the program can’t change the value stored in the variable. Instead, it means that the amount of memory that the variable takes up can never be changed. Even if you change the variable from a large value to a small one, you can’t release any of that now unnecessary memory space. You also can’t increase the amount of space available. This can be an issue if the variable is intended to store variable-length user inputs.
Functions and Subroutines
Static variables are stored in the stack rather than the heap. This means that they remain available in the instance in which they were created. Statically allocated variables in the main body of a program stay valid and in memory for the entire time the application is running. This is because the application’s stack is always there. When a separate function or class is called, it gets its record on the stack in which new statically allocated variables can be stored. The memory for these variables can be released once the class is closed, as the record is popped off the stack.
Conclusion
Static allocation is allocating memory space for a variable—the compiler locks in the amount of space needed at compile time. While the variable’s value can change, the amount of space allocated for it can’t. This limit prevents space from being freed up if it’s not needed. It also prevents more space from being made available if it is required. Static allocation does have a performance advantage over dynamic allocation but is less flexible. Typically both static and dynamic variables are used as they both offer benefits.