DEV Community

Will Peake
Will Peake

Posted on

Memory Implications of static vs non-static. Comparing pass by reference & value.

In computer programming, a static variable is a variable that retains its value between function calls and has a lifetime that is the entire duration of the program. Non-static variables, also known as automatic or stack variables, are created when a function is called and are destroyed when the function exits.

In terms of memory implications, static variables take up space in memory for the entire duration of the program, while non-static variables only take up space while the function they are declared in is running.

Pass by reference and pass by value are terms that refer to how arguments are passed to a function. In pass by value, the function receives a copy of the argument and any changes made to the argument within the function do not affect the original value of the variable outside the function. In pass by reference, the function receives a reference to the original variable and any changes made to the variable within the function will be reflected in the original variable.

In terms of memory implications, pass by reference allows the function to directly access and modify the memory of the original variable, while pass by value creates a copy of the variable that consumes additional memory.

Top comments (0)