Question

In: Computer Science

One of the following determines when the variable is created and destroyed and how long it...

One of the following determines when the variable is created and destroyed and how long it will retain its value.

linkage

storage class

scope

none of the above

Solutions

Expert Solution

Answer :

storage class

Explanation :

The storage class of a variable determines its "lifetime", how long it remains in the computer's memory. Variables in C++ essentially fall into one of three storage classes:

  • Automatic storage Automatic variables are allocated and initialized automatically when program flow enters their scope, and de-allocated automatically when program flow leaves their scope. Automatic variables are stored on the program's call stack.

    In C++, all local variables (including function and method parameters) are automatic variables by default.

  • Static storage Static variables are allocated and initialized once at the beginning of program execution. They continue to exist (and retain their value) throughout the execution of the program, regardless of whether or not they are currently in scope.

    In C++, global variables are static variables by default. A local variable or class data member (but not a function or method parameter) may be made a static variable by placing the keyword static at the front of the variable declaration.

    For example, in the following function the local variable numCalls is initialized to 0 at the start of the program. It retains its value throughout the execution of the program.

    void countCalls()
       {
       static int numCalls = 0;    // Value of numCalls is retained between each function call
    
       numCalls++;
       cout << "The countCalls function has been called " << numCalls << " times\n";
       }
    
  • Dynamic storage Dynamic variables are allocated at run time using the C++ operators new and new[]. Dynamic variables exist until they are explicitly de-allocated using the C++ operators delete and delete[] or the program ends. Dynamic variables are allocated from a large pool of unused memory area called the heap.


Related Solutions

What determines the inflation rate in the long run? How might inflation in the long run...
What determines the inflation rate in the long run? How might inflation in the long run be related to fiscal policy? What is seigniorage?
The law of the conservation of energy states that energy can neither be created or destroyed...
The law of the conservation of energy states that energy can neither be created or destroyed within a closed system, but we also know that any transfer of energy is not 100% efficient and some energy “escapes”. Write briefly about the transfer of energy within our cells …. how do we obtain, store, and use energy? Be sure to give specific examples of the way we convert chemical energy to mechanical and transport/potential energy within the cell. Also address how...
what resources are used when LWP is created and how do they differ when  HWP is created?
what resources are used when LWP is created and how do they differ when  HWP is created?
__________ re?ects the value created or destroyed each year, or in other words, the economic profit....
__________ re?ects the value created or destroyed each year, or in other words, the economic profit. a) Earnings per Share b) Capital Returns c) Ratio Analysis d) Market Value Added e) Economic Value Added
Describe the structure and function of the electoral college. •How and when was it created in...
Describe the structure and function of the electoral college. •How and when was it created in the U.S.? •Why was it created, and by whom?
What is the current federal minimum wage, how is it created, and when was the last...
What is the current federal minimum wage, how is it created, and when was the last time it was changed? Explain how the minimum wage is related to the cost of living, i.e. what is the real minimum wage?
What determines how well a regression equation will predict values of the y-variable from known values...
What determines how well a regression equation will predict values of the y-variable from known values of the x-variable? (A) What values or points make-up the regression line? (B) What two points are always found on the regression line? (Hint: These two points can be used to quickly draw the regression line) Explain why the regression line the “best fitting” line that can be plotted through the points in a scatter diagram? Describe how predicted y-values plotted near the point...
5. Which of the following is NOT considered when the government determines whether someone is unemployed?...
5. Which of the following is NOT considered when the government determines whether someone is unemployed? a. Whether the individual is 16 or older b. The wage the individual is paid c. Whether the individual has looked for work d. Whether the individual is currently employed 6. Suppose the civilian non-institutional population equals 150,000; there are 70,000 employed persons and 10,000 unemployed persons. How many people are not in the labor force? a. 10,000 b. 60,000 c. 70,000 d. 80,000
Between long straddle and long strangle, which one should a trader choose when there is a...
Between long straddle and long strangle, which one should a trader choose when there is a volatility market?
Explain what determines exchange rates in the short and long run.
Explain what determines exchange rates in the short and long run.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT