How to find Stack Area of the given C program

CWC
2 Min Read

Stack overflow is an error in the C programming language. It happens when there is no space left in the stack. In other words, the memory gets filled up.

Stack is a temporary memory used to store the data while it is being processed. The data will be pushed to the stack and once the processing is completed the data will be popped out of the stack.

This error occurs when the data exceeds the capacity of the stack. This will make the system crash and the program will end without showing any error message.

Stack is a temporary memory used to store the data while it is being processed.

The data will be pushed to the stack and once the processing is completed the data will be popped out of the stack.

C Program for Stack Area


#include
int *fun()
{
int b = 10; return &b;
}
int main()
{
int *p;
p = fun(); printf("%d\n", *p); return 0;
}

After the fun() execution is completed the addresses allocated to fun() are also destroyed, and the space allocated for the fun() in the stack area becomes ready for the next function call.

Thus the address of the variable “b” is no longer relevant after the execution of the fun(). If we attempt at accessing ‘b’ there is no assurance that the value allocated to ‘b’ remains in existence or not. Sometimes , it could be there or it could be replaced by functions. This is dependent on the programming of the compiler.

As in Turbo C, it gives an error, but in the gcc compiler , it only gives a warning.

Share This Article
Leave a comment

Leave a Reply

Your email address will not be published. Required fields are marked *

English
Exit mobile version