Question

In: Computer Science

could you explain stack frame and local variables saved on stack. also explain recursive programs --...

could you explain stack frame and local variables saved on stack.

also explain recursive programs -- what is stack over flow? What is a stopping case for recursive codes?

Solutions

Expert Solution

A stack frame sometimes also referred to as an activation record is the collection of all the data on the stack associated with one subprogram call.

In simple terms whenever we double-click on any program in our system to run it, it gets loaded into the main memory for execution. Now the program is loaded as a stack into the memory with various subparts, where each subpart contains specific things for some specific purposes. Each part in some way helps in the execution of the program. Some of the parts of a stack frame can include:- The return address, Local variables, program registers, and so on.

Local variables are the variables that have a local scope which means that they are defined inside the functions. These are the variables in High-level language that are not declared as static. These actually reside in the stack frame and have an automatic storage class. When the program enters a block such as a function or a method, space is allocated on top of the stack for the local variables.

Recursive programs:- We know that a program can also include functions. A function is a reusable piece of code that can be used again by simply calling the name of the function. A recursive program is a program that contains a special kind of such function called as a recursive function. A recursive function is one that calls itself in its function definition.

A recursive function must have two parts:-

a) A general case, i.e., the case for which the solution is given in terms of a smaller version of the current problem.

b) A base case, where the program comes and stop making further calls to itself. This is the stopping case for a recursive program. Without this, a recursive program will keep calling itself and until the program or the system itself crashes.

Stack Overflow:- It is a condition when a function or program uses more memory than is in the stack. As it grows beyond its allocated space, the dynamic stack contents begin to overwrite other things, such as critical application code and data. A stack overflow condition will lead the program to crash. There are two conditions which can lead to stack overflow:-

a) First the most obvious is if we declare a very large no. of local variables or declare an array or matrix or any higher dimensional array of large size can result in overflow of the stack.

b) If a function recursively calls itself infinite times then the stack is unable to store large no.of local variables used by every function call and will result in overflow of the stack. This will happen when our recursive function does not have a termination condition.

Hope this helps. If you have any queries or suggestions regarding the answers please leave them in the comments section so I can update and improve the answer. Thank you.


Related Solutions

You tell a family that their child’s life could be saved with a treatment, but they...
You tell a family that their child’s life could be saved with a treatment, but they refuse based on religious reasons. Do you (A)- perform the treatment without their permission or (B)- honor their request?   Summarize consequential (Utilitarianism) ethical theory in at least one paragraph. Summarize non-consequential (Deontology) ethical theory in at least one paragraph. Apply utilitarianism and deontology theory to scenario
You tell a family that their child’s life could be saved with a treatment, but they...
You tell a family that their child’s life could be saved with a treatment, but they refuse based on religious reasons. Do you (A)- perform the treatment without their permission or (B)- honor their request? Summarize consequential (Utilitarianism) ethical theory in at least one paragraph. Summarize non-consequential (Deontology) ethical theory in at least one paragraph. Apply utilitarianism and deontology theory to scenario
In this module you learned how to implement recursive functions in your C++ programs. For this...
In this module you learned how to implement recursive functions in your C++ programs. For this assignment, you will create a program that tests a string to see if it is a palindrome. A palindrome is a string such as “madam”, “radar”, “dad”, and “I”, that reads the same forwards and backwards. The empty string is regarded as a palindrome. Write a recursive function: bool isPalindrome(string str, int lower, int upper) that returns true if and only if the part...
could you explain the differences and the similarities between undetermined coefficients method and annihilator method? also,...
could you explain the differences and the similarities between undetermined coefficients method and annihilator method? also, could you please solve an example step by step to explain how the annihilator method works?
Explain (in your own words) what is Lead Poisoning Prevention and Healthy Homes Programs. Also, explain...
Explain (in your own words) what is Lead Poisoning Prevention and Healthy Homes Programs. Also, explain why it is important to get babies and pregnant women tested for lead poisoning. (NO PLAGIARIZING 300-500 WORDS)  
When you determine if there is an association between two variables, it is also important for...
When you determine if there is an association between two variables, it is also important for you to determine how strong or weak that association is. This is why, when you have data for two quantitative variables, you calculate what is called the coefficient for correlation. Instructions Suppose you are determining the association between the weight of a car and the miles per gallon that the car gets. Answer the following questions in a Word document: define correlation and explain...
Could you please write "linkedlist.cpp" using recursive approach to manage a linked list for the given...
Could you please write "linkedlist.cpp" using recursive approach to manage a linked list for the given files below as well as an updated makefile. // app.cpp #include <iostream> #include "linkedlist.h" using namespace std; void find(LinkedList& list, char ch) { if (list.find(ch)) cout << "found "; else cout << "did not find "; cout << ch << endl; } int main() { LinkedList list; list.add('x'); list.add('y'); list.add('z'); cout << list; find(list, 'y'); list.del('y'); cout << list; find(list, 'y'); list.del('x'); cout <<...
if you could order a cut of $700 billion in federal spending, which programs would you...
if you could order a cut of $700 billion in federal spending, which programs would you cut and why would you cut them? Be specific about the programs, the dollar amounts, and your reasoning.
Explain the difference in the C memory model between global and local variables. How is each...
Explain the difference in the C memory model between global and local variables. How is each allocated and accessed?
Could you please write a working program "linkedlist.cpp" using recursive approach for the given files below...
Could you please write a working program "linkedlist.cpp" using recursive approach for the given files below -------------------------------------------------------------------------------------------- // app.cpp #include <iostream> #include "linkedlist.h" using namespace std; void find(LinkedList& list, char ch) { if (list.find(ch)) cout << "found "; else cout << "did not find "; cout << ch << endl; } int main() { LinkedList list; list.add('x'); list.add('y'); list.add('z'); cout << list; find(list, 'y'); list.del('y'); cout << list; find(list, 'y'); list.del('x'); cout << list; find(list, 'y'); list.del('z'); cout << list;...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT