Codes / Error Code 017
Codes / Error Code 017
Error Code 017
Overview
This error occurs when the call stack is exhausted, typically due to excessive recursion or an infinite loop involving function calls. A stack overflow usually leads to program crashes or runtime errors.
Details
-
Common Causes:
- Infinite recursion without a proper base case.
- Allocating excessively large local variables or arrays.
- Deeply nested function calls that exceed stack limits.
-
Example:
void infiniteRecursion() {
infiniteRecursion(); // Error: stack overflow
}
void main() {
infiniteRecursion();
}
- Solution:
- Ensure recursive functions have a valid base case to terminate recursion:
int factorial(int n) { if (n <= 1) { return 1; // Base case } return n * factorial(n - 1); } void main() { writeln(factorial(5)); // Safe recursion } - Avoid deep recursion by using iterative methods when possible:
int factorialIterative(int n) { int result = 1; for (int i = 1; i <= n; i++) { result *= i; } return result; } void main() { writeln(factorialIterative(5)); // Safe iteration } - Limit the size of local variables to prevent excessive stack usage.
- Ensure recursive functions have a valid base case to terminate recursion: