CS201 Introduction to Programming GDB Solution Spring 2014

GDB topic:

Is it more expensive to use too many static variables instead of ordinary variables? If yes, then how? Your answer must be concise and not be more than 200 words.

Note: Keep you comment brief and to the point, GDB will not be accepted through email in any case.

Solution: it would be more expensive to use static variables instead ordinary variables due to the fact that automatic variables are created and destroyed when they go in and out of scope (so memory is freed after the program is out of the block in which a variable is declared). Whereas static variables are stored until the program terminates – meaning they remain allocated even if you don’t need them in your program after a certain point; which is “wasted” memory. 

Static variables are expensive :-
1.  it remains in memory ,the entire run time of the program.


2. static variables are not thread safe.


3. it violates the principle of information hiding(encapsulation)


4. it is difficult to test the code with static variables.