Stack overflow happens when the stack pointer exceeds the stack bound. As you can think of it the max amount of memory provided by the system to run your program within that provided memory.
The call stack may consist of a limited amount of address space, often determined at the start of the program. The size of the call stack depends on many factors, including the programming language, machine architecture, multi-threading, and amount of available memory.
When a program attempts to use more space than is available on the call stack (that is, when it attempts to access memory beyond the call stack's bounds, which is essentially a buffer overflow), the stack is said to overflow, typically resulting in a program crash.
Condition which can cause the stack overflow :
1.Infinite recursion, For example.
main(); // Here calling main() itself until stack overflow
Allocating a memory which is bigger the stock memory allocated to you.
double temp; //Something like that. if it is not giving the segfault/code crash increase the value.