987 views

3 Answers

0 votes
0 votes
stackoverflow means when we run short of memory...it occurs mostly in recursive programs..which don't have termination condition.
0 votes
0 votes

stack overflow occurs if the call stack pointer exceeds the stack bound. the program uses to store information during a function call about where to return to. Each time you call a function, the CPU saves the location of what's currently executing onto the stack, then jumps to the new function. When the function is done, it pops that location off the stack and returns there. This is what makes recursion possible.

When a program attempts to use more space than is available on the call stack, typically resulting in a program crash. Possible causes are Infinite recursion, Very deep recursion or Very large stack variables.

The actual size of the stack is completely platform-dependent. Many operating systems tend to limit the size of the stack to a few megabytes, but allow that size to be changed. If the system runs out of virtual memory, this can also limit the stack size. Some (much) older CPUs have a fixed-size stack (the Intel 8008 only has 7 stack entries).

If we run this program, compiler will print "Stack Overflow" until stack overflows.

#include‬<stdio.h>
int main()
{
    printf("Stack Overflow");
    main();
}
0 votes
0 votes

The stack memory is used to store local variables and function call, while heap memory is used to store objects.If there is no memory left in stack for storing function call or local variable,then it leads to stackoverflow error.Common eg for this would be recursive function because it stores information about the function (information includes where it left or called itself again) in stack.

If you have a function like:

<code>int foo()
{
    // more stuff
    foo();
}</code>

Then foo() will keep calling itself, getting deeper and deeper, and when the space used to keep track of what functions you're in is filled up, you get the stack overflow error.

In more detail:

Usually there's a one stack for a process(program in the running state).That stack tends to be a fixed memory range somewhere in the memory, therefore it's limited how much it can contain values.

If the stack is empty you can't pop, if you do you'll get stack underflow error.

If the stack is full you can't push, if you do you'll get stack overflow error.

So stack overflow appears where you allocate too much into the stack.

Related questions

0 votes
0 votes
1 answer
1
1 votes
1 votes
1 answer
3
2 votes
2 votes
3 answers
4
Laahithyaa VS asked Sep 9, 2023
999 views
. What will be the value returned by the following function, when it is called with 11?recur (int num){if ((num / 2)! = 0 ) return (recur (num/2) *10+num%2);else return 1...